简体   繁体   English

错误:扩展方法必须在非通用静态类中定义

[英]Error: Extension method must be defined in a non-generic static class

I get the following compilation error at the class name. 我在类名处收到以下编译错误。

Extension method must be defined in a non-generic static class

I am not using normal class. 我不在上普通课。 What could be the reason for this. 这可能是什么原因。 I don't know and don't want to use extension methods. 我不知道,也不想使用扩展方法。

As requested, here is my comment as an answer: 根据要求,以下是我的评论作为答复:

Without your code there isn't much we can do. 没有您的代码,我们将无能为力。 My best guess is that you accidentally typed "this" somewhere in a parameter list. 我最好的猜测是您不小心在参数列表中的某个地方键入了“ this”。

Sample for extension method 扩展方法样本

public static class ExtensionMethods {
 public static object ToAnOtherObject(this object obj) {
  // Your operation here
 }
}

I had the same problem, and solved it as follows. 我遇到了同样的问题,并按如下解决。 My code was something like this: 我的代码是这样的:

public static class ExtensionMethods 
{
    public static object ToAnOtherObject(this object obj) 
    {
        // Your operation here
    }
}

and I changed it to 然后我将其更改为

public static class ExtensionMethods 
{
    public static object ToAnOtherObject(object obj) 
    {
        // Your operation here
    }
}

I removed the word "this" of the parameter of the method. 我删除了方法参数的单词“ this”。

I'm guessing this relates to your previous list question ; 我猜这与您以前的清单问题有关 if so, the example I provided is an extension method, and would be: 如果是这样,我提供的示例扩展方法,将是:

public static class LinkedListUtils { // name doesn't matter, but must be
                                      // static and non-generic
    public static IEnumerable<T> Reverse<T>(this LinkedList<T> list) {...}
}

This utility class does not need to be the same as the consuming class, but extension methods is how it is possible to use as list.Reverse() 该实用工具类不必与消费类相同,但是扩展方法是可以用作list.Reverse()

If you don't want it as an extension method, you can just make it a local static method - just take away the "this" from the firstparameter: 如果您不希望将其用作扩展方法,则可以将其设为本地静态方法-只需从firstparameter中删除“ this”即可:

public static IEnumerable<T> Reverse<T>(LinkedList<T> list) {...}

and use as: 并用作:

foreach(var val in Reverse(list)) {...}

The following points need to be considered when creating an extension method: 创建扩展方法时,需要考虑以下几点:

  1. The class which defines an extension method must be non-generic and static 定义扩展方法的类必须是非泛型static
  2. Every extension method must be a static method 每个扩展方法都必须是static方法
  3. The first parameter of the extension method should use the this keyword. 扩展方法的第一个参数应使用this关键字。

How about posting your code? 如何发布您的代码? Extension methods are declared by preceding the first parameter of a static method with this. 扩展方法是通过在静态方法的第一个参数之前添加此方法来声明的。 Since you don't won't to use an extension method, I suspect you accidentally started a parameter list with this . 由于您不会使用扩展方法,因此我怀疑您不小心使用this启动参数列表。

Look for something like: 寻找类似的东西:

void Method(this SomeType name)
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM