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

本文关键字:泛型 静态类 定义 方法 扩展 | 更新日期: 2023-09-27 17:57:29

我得到错误:

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

在线:

public class LinqHelper

这是基于MarkGavells代码的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;
    }
}

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

更改

public class LinqHelper

public static class LinqHelper

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

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

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

在类声明中添加关键字static

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

为遇到Nathan这样的错误的人提供的变通方法:

动态编译器似乎有此扩展方法错误的问题。。。添加static对我也没有帮助。

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

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

我认为这个线程已经获得了足够的视图,值得把我找到的(有限的)解决方案传下去。大多数人可能尝试在谷歌搜索解决方案之前添加"static"!我在其他任何地方都没有看到这种修复方法。

尝试更改

public class LinqHelper

 public static class LinqHelper

我被这个编译器错误弄糊涂了。我的类不是一个扩展方法,几个月以来一直运行良好,需要保持非静态。我在课堂上加入了一个新方法:

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

我从一个样本中复制了这种方法,但没有注意到";这个";方法签名中的修饰符,用于扩展方法。删除它解决了问题。

将其更改为

public static class LinqHelper

我在将项目转换为使用依赖项注入时遇到了这个问题。如果方法声明包含"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",则可以将这些方法移动到单独的公共静态类中。

Extension方法应该在静态类内部。因此,请在一个静态类中添加您的扩展方法。

例如,它应该像这个

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

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

我遇到了类似的问题,我创建了一个"foo"文件夹,并在foo中创建了"class",然后我得到了前面提到的错误。一个修复方法是将前面提到的"static"添加到将成为"public static class LinqHelper"的类中。

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

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

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