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

时间:2011-05-23 11:00:32

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

我收到了错误:

  

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

在线:

public class LinqHelper

这是基于Mark Gavells代码的助手类。我真的很困惑这个错误意味着什么,因为我确信它在星期五离开时工作正常!

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;
    }
}

10 个答案:

答案 0 :(得分:273)

更改

public class LinqHelper

public static class LinqHelper

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

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

答案 1 :(得分:20)

将关键字static添加到类声明:

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

答案 2 :(得分:16)

尝试更改

public class LinqHelper

 public static class LinqHelper

答案 3 :(得分:15)

将其更改为

public static class LinqHelper

答案 4 :(得分:11)

为遇到像Nathan这样的错误的人解决方法:

即时编译器似乎有这个扩展方法错误的问题...添加static也没有帮助我。

我想知道导致这个错误的原因是什么?

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

认为这个帖子已经获得了足够多的观点,我发现它是值得传递的(有限的)解决方案。大多数人可能都试过添加静电&#39;在谷歌寻求解决方案之前!而我在其他任何地方都没有看到这种解决方法。

答案 5 :(得分:8)

如果您不打算使用静态函数,则只需删除参数中的“ this”关键字即可。

答案 6 :(得分:2)

我为这个编译器错误抓狂了。我的课程不是不是扩展方法,几个月以来一直运行良好,需要保持非静态状态。我在类中加入了一个新方法:

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

我从一个样本中复制了该方法,但没有注意到方法签名中的“ this”修饰符,该修饰符用于扩展方法中。删除它可以解决问题。

答案 7 :(得分:1)

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

所以例如它应该是这样的

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;
        }

    }

答案 8 :(得分:0)

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

答案 9 :(得分:0)

我遇到了类似的问题,我在foo中创建了一个“ foo”文件夹并创建了一个“类”,然后出现了上述错误。一种解决方法是将前面提到的“ static”添加到将成为“ public static class LinqHelper”的类中。

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

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

WORKAROUND如果您不需要静态。 我的解决方法是直接在名称空间下创建一个类,然后将其拖到“ foo”文件夹中。