Search code examples
c#code-analysisvisual-studio-2022

Disable IDE0002 rule in some cases?


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


Solution

  • 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;
        }
    }