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

我得到了错误:

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

在线上:

public class LinqHelper

下面是基于Mark Gavells代码的helper类。我真的很困惑这个错误意味着什么,因为我确信它在周五离开时工作得很好!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Linq.Expressions;
using System.Reflection;


/// <summary>
/// Helper methods for link
/// </summary>
public class LinqHelper
{
public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderBy");
}
public static IOrderedQueryable<T> OrderByDescending<T>(this IQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "OrderByDescending");
}
public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenBy");
}
public static IOrderedQueryable<T> ThenByDescending<T>(this IOrderedQueryable<T> source, string property)
{
return ApplyOrder<T>(source, property, "ThenByDescending");
}
static IOrderedQueryable<T> ApplyOrder<T>(IQueryable<T> source, string property, string methodName)
{
string[] props = property.Split('.');
Type type = typeof(T);
ParameterExpression arg = Expression.Parameter(type, "x");
Expression expr = arg;
foreach (string prop in props)
{
// use reflection (not ComponentModel) to mirror LINQ
PropertyInfo pi = type.GetProperty(prop);
expr = Expression.Property(expr, pi);
type = pi.PropertyType;
}
Type delegateType = typeof(Func<,>).MakeGenericType(typeof(T), type);
LambdaExpression lambda = Expression.Lambda(delegateType, expr, arg);


object result = typeof(Queryable).GetMethods().Single(
method => method.Name == methodName
&& method.IsGenericMethodDefinition
&& method.GetGenericArguments().Length == 2
&& method.GetParameters().Length == 2)
.MakeGenericMethod(typeof(T), type)
.Invoke(null, new object[] { source, lambda });
return (IOrderedQueryable<T>)result;
}
}
394828 次浏览

在类声明中添加关键字static:

// this is a non-generic static class
public static class LinqHelper
{
}

改为

public static class LinqHelper

试着改变

public class LinqHelper

 public static class LinqHelper

改变

public class LinqHelper

public static class LinqHelper

在创建扩展方法时,需要考虑以下几点:

  1. 定义扩展方法的类必须是non-genericstaticnon-nested
  2. 每个扩展方法必须是static方法
  3. 扩展方法的第一个参数应该使用this关键字。

对于像Nathan这样遇到bug的人来说,有一个解决方案:

在飞行编译器似乎有这个扩展方法错误的问题…添加static也没有帮助我。

我想知道是什么导致了这个错误?

但是解决方法是在同一个文件中编写一个新的扩展类(不嵌套)并重新构建。

我认为这个线程获得了足够多的视图,值得传递我找到的(有限的)解决方案。大多数人可能会在谷歌搜索解决方案之前尝试添加“static”!我在其他地方也没见过这种变通办法。

扩展方法应该在静态类中。 所以请在静态类中添加你的扩展方法

比如它应该是这样的

public static class myclass
{
public static Byte[] ToByteArray(this Stream stream)
{
Int32 length = stream.Length > Int32.MaxValue ? Int32.MaxValue : Convert.ToInt32(stream.Length);
Byte[] buffer = new Byte[length];
stream.Read(buffer, 0, length);
return buffer;
}


}

尝试将其更改为静态类,然后返回。这可能会解决视觉工作室在误报时的抱怨。

如果你不打算使用静态函数,只需去掉参数中的"this"关键字。

我遇到了类似的问题,我创建了一个“foo”文件夹,并在foo内部创建了一个“类”,然后我得到上述错误。一个解决办法是添加“静态”,如前面提到的类将是“公共静态类LinqHelper”。

我的假设是,当你在foo文件夹中创建一个类时,它会将其视为一个扩展类,因此以下规则适用于它:

1)每个扩展方法都必须是静态方法

WORKAROUND如果你不想要静电。 我的解决方法是直接在命名空间下创建一个类,然后将其拖放到"foo"文件夹中

我对这个编译错误百思不得其解。我的类是不是一个扩展方法,几个月以来一直工作得很好,需要保持非静态。我在类中包含了一个新方法:

private static string TrimNL(this string Value)
{...}

我从一个样本中复制了这个方法,没有注意到“this”。方法签名中的修饰符,在扩展方法中使用。去掉它就解决了这个问题。

我在将一个项目转换为使用依赖注入时遇到了这种情况。如果方法声明中包含" this "关键字,VS将给出警告。在我的例子中,我能够从所有的方法声明中删除“this”。如果需要使用“this”,则必须将其设置为静态。

 public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string property)

更改为

     public static IOrderedQueryable<T> OrderBy<T>(IQueryable<T> source, string property)

如果您希望避免使用带有静态方法的静态类,则可能需要在编写代码时避免在方法声明中使用“this”关键字。如果某些方法只需要“this”,那么这些方法可以被移动到一个单独的公共静态类中。

如果类中的任何方法有[this]关键字,visual studio会认为你正在构建一个扩展,所以从类中的每个方法中删除[this]关键字,错误就会消失