C# 代码分析 CA1822 警告 - 为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2229768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:34:14  来源:igfitidea点击:

C# Code Analysis CA1822 Warning - Why?

c#code-analysis

提问by Randy Minder

I have the method shown below which is generating a CA1822 Code Analysis warning. CA1822 says this:

我有下面显示的方法,它会生成 CA1822 代码分析警告。CA1822 是这样说的:

"The 'this parameter (or 'Me' in Visual Basic) of 'ImportForm.ProcessFile(StreamReader)' is never used. Mark the member as static (or Shared in Visual Basic) or use 'this/Me' in the method body or at least one property accessor, if appropriate."

"The 'this parameter (or 'Me' in Visual Basic) of 'ImportForm.ProcessFile(StreamReader)' is never used. Mark the member as static (or Shared in Visual Basic) or use 'this/Me' in the method body or at least one property accessor, if appropriate."

Can anyone tell me why I am getting this warning, since the 'reader' parameter is in fact being used?

谁能告诉我为什么会收到此警告,因为实际上正在使用“阅读器”参数?

private void ProcessFile(StreamReader reader)
{
   string[] lines;

   lines = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

   ParseFile.IVAFile(lines);
}

采纳答案by Jeff Foster

It means you use no members of the object. All the items in the method come from the parameters.

这意味着您不使用对象的成员。方法中的所有项都来自参数。

Therefore the method can safely be made static.

因此,该方法可以安全地设为静态。

回答by Jon Skeet

"reader" is being used, but you're not using "this" anywhere, so you can make the method static.

正在使用“reader”,但您没有在任何地方使用“this”,因此您可以使方法静态化。

The only reason notto make it static would be if you want to use polymorphism later - e.g. making it virtual and overriding it elsewhere.

让它成为静态的唯一原因是如果你想稍后使用多态——例如,让它成为虚拟的并在其他地方覆盖它。

回答by Rob Levine

I think it is trying to tell you that this method can be made static.

我认为它试图告诉你这个方法可以是静态的。

The only thing this method needs to access is "reader", but nothing from the class instance to which it belongs ("this"). In which case, you can safely make it static.

这个方法唯一需要访问的是“reader”,但它所属的类实例(“this”)没有任何东西。在这种情况下,您可以安全地将其设为静态。

回答by Simon Linder

The warning occurs because you don't use any member variables of that class in that method. E.g.

出现警告是因为您没有在该方法中使用该类的任何成员变量。例如

this.m_anyVariable = anyValue;

Therefore you can/should mark that method as static.

因此,您可以/应该将该方法标记为静态。

回答by Holger

Maybe I have found malicious behavior of this message.

也许我发现了这条消息的恶意行为。

In a Situation like

在这样的情况下

void Print()
{
    Console.Writeline(GetType().Name);
}

I get this CA1822reported, although GetType()is an instance method. However, I found some Explanation, why GetType()is actually not a virtual method, not an instead method, and technicly behaving like a static method.

我得到了这个CA1822报告,虽然GetType()是一个实例方法。但是,我找到了一些解释,为什么GetType()实际上不是虚拟方法,不是替代方法,并且在技术上表现得像静态方法。

It's just that the code Analysis does not consider this special behavior.

只是代码分析没有考虑这种特殊行为。