如何为方法的所有调用者禁用代码分析规则
How to disable code analysis rule for all callers of a method
鉴于这两个 classes:
public class Abc
{
public static void Method(string propertyName) { }
}
public class Def
{
public int Prop { get; }
public void Method2() { Abc.Method("Prop"); }
}
原样,将为 Method2
触发 Roslyn 规则 CA1507(使用 nameof)。我不希望这样,因为该字符串用于长期自定义序列化并且永远不会更改(如果我们决定更改 Prop
的名称,我们将不会更改该字符串)。我不想在程序集级别甚至 class 级别禁用规则。还有数百个像 Def
这样的呼叫者,所以我想要一些不需要我对呼叫者做任何事情的东西。
是否有某种 [ExcludeParameterFromCodeAnalysis] 我可以将 propertyName
参数从所有或部分代码分析中排除?
这是我希望存在的概念,或者它的一些变体:
public class Abc
{
public static void Method([SuppressMessageForCallers("CA1507")]string propertyName) { }
}
public class Def
{
public int Prop { get; }
public void Method2() { Abc.Method("Prop"); }
}
关于此警告,Abc.Method
声明无能为力,因为警告不在方法上(甚至在它的调用上),而是在文字本身上。
它可能很难看,但它有效:
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 将在左侧排水沟的灯泡或螺丝刀菜单上提供。
我相信这条规则只适用于 triggers1 当你的参数名称是 paramName
或 propertyName
2。所以让我们改变参数:
public class Abc
{
public static void Method(string propertySerializationName) { }
}
1即使您不知道或无法猜测是哪个特定的分析器实施了警告,看起来也像是在 roslyn-analyzers 存储库中搜索特定代码( CA1507
) 应该可以帮助您在没有太多误报的情况下找到它们。
2奇怪的是,它甚至不会触发名为 parameterName
.
的参数
鉴于这两个 classes:
public class Abc
{
public static void Method(string propertyName) { }
}
public class Def
{
public int Prop { get; }
public void Method2() { Abc.Method("Prop"); }
}
原样,将为 Method2
触发 Roslyn 规则 CA1507(使用 nameof)。我不希望这样,因为该字符串用于长期自定义序列化并且永远不会更改(如果我们决定更改 Prop
的名称,我们将不会更改该字符串)。我不想在程序集级别甚至 class 级别禁用规则。还有数百个像 Def
这样的呼叫者,所以我想要一些不需要我对呼叫者做任何事情的东西。
是否有某种 [ExcludeParameterFromCodeAnalysis] 我可以将 propertyName
参数从所有或部分代码分析中排除?
这是我希望存在的概念,或者它的一些变体:
public class Abc
{
public static void Method([SuppressMessageForCallers("CA1507")]string propertyName) { }
}
public class Def
{
public int Prop { get; }
public void Method2() { Abc.Method("Prop"); }
}
关于此警告,Abc.Method
声明无能为力,因为警告不在方法上(甚至在它的调用上),而是在文字本身上。
它可能很难看,但它有效:
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 将在左侧排水沟的灯泡或螺丝刀菜单上提供。
我相信这条规则只适用于 triggers1 当你的参数名称是 paramName
或 propertyName
2。所以让我们改变参数:
public class Abc
{
public static void Method(string propertySerializationName) { }
}
1即使您不知道或无法猜测是哪个特定的分析器实施了警告,看起来也像是在 roslyn-analyzers 存储库中搜索特定代码( CA1507
) 应该可以帮助您在没有太多误报的情况下找到它们。
2奇怪的是,它甚至不会触发名为 parameterName
.