2

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

aybe
  • 15,516
  • 9
  • 57
  • 105
lisz
  • 435
  • 5
  • 9
  • Please read [ask], especially the bullet saying, "**DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question." – Heretic Monkey Mar 24 '22 at 00:27
  • Does this answer your question? [Disable "Name can be simplified" IDE0003 fix hint](https://stackoverflow.com/questions/48857982/disable-name-can-be-simplified-ide0003-fix-hint) – gunr2171 Mar 24 '22 at 00:31
  • See also [Suppressing issues from Roslyn code Analyzers](https://stackoverflow.com/questions/34720443/suppressing-issues-from-roslyn-code-analyzers) – gunr2171 Mar 24 '22 at 00:31
  • I don't think so, because I don't want to disable this rule, just make it not "fire" in some cases – lisz Mar 24 '22 at 00:40

1 Answers1

4

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;
    }
}
aybe
  • 15,516
  • 9
  • 57
  • 105