Reputation: 697
Given these two classes:
public class Abc
{
public static void Method(string propertyName) { }
}
public class Def
{
public int Prop { get; }
public void Method2() { Abc.Method("Prop"); }
}
As is, Roslyn rule CA1507 (use nameof) will be triggered for Method2
. I don't want that, because that string is used for long-term custom serialization and can never change (if we decide to change the name of Prop
we won't be changing that string). I don't want to disable the rule on an assembly level or even class level. There are also hundreds of callers like Def
so I want something that doesn't require me to do anything to the callers.
Is there some kind of [ExcludeParameterFromCodeAnalysis] I can put on the propertyName
param to be excluded from all or some code analysis?
Here's the concept I hope exists, or some variant on it:
public class Abc
{
public static void Method([SuppressMessageForCallers("CA1507")]string propertyName) { }
}
public class Def
{
public int Prop { get; }
public void Method2() { Abc.Method("Prop"); }
}
Upvotes: 1
Views: 350
Reputation: 239664
I believe that this rule only triggers1 when the name of your parameter is paramName
or propertyName
2. So let's change the parameter:
public class Abc
{
public static void Method(string propertySerializationName) { }
}
1Even if you don't know or can guess at which specific analyzer implements a warning, it looks like searching the roslyn-analyzers repository for the specific code (CA1507
) should help you find them without too many false positives.
2Weirdly, it wouldn't even appear to trigger on a parameter called parameterName
.
Upvotes: 3
Reputation: 14846
There's nothing that can be done to Abc.Method
declaration regarding this warning because the warning is not on the method (or even on it's invocation) but on the literal itself.
It might be ugly, but it works:
public class Abc
{
public static void Method(string propertyName) { }
}
public class Def
{
public int Prop { get; }
public void Method2()
{
#pragma warning disable CA1507 - use nameof
Abc.Method("Prop");
#pragma warning restore CA1507 - use nameof
}
}
Visual Studio will offer that on the light bulb or screwdriver menu on the left gutter.
Upvotes: 0