Home > OS >  Disable IDE0002 rule in some cases?
Disable IDE0002 rule in some cases?

Time:03-24

Can you somehow fine-tune the rule "Simplify member access (IDE0002)" in Visual Studio 2022? It's mostly useful, until it's not :)

internal class Good_God_Please_Help_Why_Is_This_Class_Name_So_Looooooooooooooooooooooooooooooooooong {

    internal class SomeConstants {
        public const string A = "A";
        public const string B = "B";
    }
}

internal class Salvation : Good_God_Please_Help_Why_Is_This_Class_Name_So_Looooooooooooooooooooooooooooooooooong { }

internal class Test {
    void SomeMethod() {
        var a = Salvation.SomeConstants.A;
    }
}

Code above produces following simplification:

IDE0002 gone wild

CodePudding user response:

Use SuppressMessage:

using System.Diagnostics.CodeAnalysis;

internal class Test
{
    [SuppressMessage("Style", "IDE0002")]
    private void SomeMethod()
    {
        var a = Salvation.SomeConstants.A;
    }
}

Before:

enter image description here

After:

enter image description here

Alternatively, import static member:

using static Good_God_Please_Help_Why_Is_This_Class_Name_So_Looooooooooooooooooooooooooooooooooong;

internal class Test
{
    private void SomeMethod()
    {
        var a = SomeConstants.A;
    }
}
  • Related