编译器错误 CS1113Compiler Error CS1113

无法使用值类型“name”上定义的扩展方法“name”来创建委托。Extension methods 'name' defined on value type 'name' cannot be used to create delegates.

无法使用为类类型定义的扩展方法来创建委托。Extension methods that are defined for class types can be used to create delegates. 无法使用为值类型定义的扩展方法。Extension methods that are defined for value types cannot.

更正此错误To correct this error

  1. 将扩展方法与类类型关联。Associate the extension method with a class type.

  2. 使该方法成为结构上的常规方法。Make the method a regular method on the struct.

示例Example

以下示例生成 CS1113:The following example generates CS1113:

// cs1113.cs  
using System;  
public static class Extensions  
{  
    public static S ExtMethod(this S s)  
    {  
        return s;  
    }  
}  
  
public struct S  
{  
}  
  
public class Test  
{  
    static int Main()  
    {  
        Func<S> f = new S().ExtMethod; // CS1113  
        return 1;  
    }  
}  

请参阅See also