错误:扩展方法必须在非泛型静态类中定义 [英] Error :Extension method must be defined in a non-generic static class

查看:174
本文介绍了错误:扩展方法必须在非泛型静态类中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处输入图片描述公共类countryController:控制器 //错误:扩展方法必须在非通用静态类中定义

enter image description here public class countryController : Controller //Error :Extension method must be defined in a non-generic static class

推荐答案

您不能在非静态类countryController

不允许这样做:

public class MyExtensions
{
    public static void SomeExtension(this String str)
    {

    }
}

这是允许的:

public static class MyExtensions
{
    public static void SomeExtension(this String str)
    {

    }
}

您有一个方法的第一个参数以this开头,您需要找到它并通过删除this对其进行修改,或者将其移至static助手类.

You have a method whose first parameter starts with this, you need to find it and either modify it by removing the this or move it to a static helper class.

根据C#规范:

10.6.9扩展方法

10.6.9 Extension methods

当方法的第一个参数包含this修饰符时, 方法被称为扩展方法.扩展方法只能 在非通用,非嵌套的静态类中声明.首先 扩展方法的参数只能有修饰符 这,并且参数类型不能是指针类型.

When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a pointer type.

这篇关于错误:扩展方法必须在非泛型静态类中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆