c# - 必须在非泛型静态类中定义扩展方法

标签 c# .net compiler-errors extension-methods

错误:

public partial class Form2 : Form

可能的原因:

public static IChromosome To<T>(this string text)
{
    return (IChromosome)Convert.ChangeType(text, typeof(T));
}

尝试(没有静态关键字):

public IChromosome To<T>(this string text)
{
    return (IChromosome)Convert.ChangeType(text, typeof(T));
}

最佳答案

如果您从参数中删除“this”,它应该可以工作。

public static IChromosome To<T>(this string text)

应该是:

public static IChromosome To<T>(string text)

关于c# - 必须在非泛型静态类中定义扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10412233/

相关文章:

c# - 如何分析慢查询?

.net - PaintEventArgs.ClipRectangle 和 PaintEventArgs.Graphics.ClipBounds 有什么区别

.net - 如果我调用WaitHandle.Dispose(),正在等待的线程会怎样?

compiler-errors - 为自定义MATLAB工具箱执行编译器脚本时出错

java - 我在为Greenfoot MyWorld类设置颜色时遇到麻烦

c# - 了解 Web.config 文件中的 ASP.NET 程序集引用管理

c# - 您如何在 VS 扩展中强制调用 ILineTransformSource.GetLineTransform 以更改线条装饰尺寸?

.net - 如何在 C# 枚举中使用特殊字符?

c++ - 重载时未在 C++ 中的范围错误中声明

c# - COM Interop 未通过接口(interface)检测 .NET 函数