Compiler Error CS1112

不要使用“System.Runtime.CompilerServices.ExtensionAttribute”。请改用“this”关键字。

当在包含扩展方法的非静态类上使用 ExtensionAttribute 时,会生成此错误。如果在静态类上使用此特性,则可能会发生其他错误,如 CS0708:“不能在静态类中声明实例成员”。

在 C# 中,必须在静态类中定义扩展方法,并用 this 关键字修饰该方法的第一个参数。任何情况下都不要在源代码中使用该特性。有关更多信息,请参见扩展方法(C# 编程指南)

更正此错误

  1. 移除该特性,然后将 this 修饰符应用于该方法的第一个参数。

下面的示例生成 CS1112:

// cs1112.cs
[System.Runtime.CompilerServices.ExtensionAttribute] // CS1112
public class Extensions
{
    public bool A(bool b) { return b; }
}

class A { }