首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >必须在非泛型静态类中定义扩展方法

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

Stack Overflow用户
提问于 2011-05-23 19:00:32
回答 11查看 358.8K关注 0票数 242

我得到了一个错误:

必须在非泛型静态类中定义

扩展方法

在线上:

代码语言:javascript
复制
public class LinqHelper

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

代码语言:javascript
复制
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;
    }
}
EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2011-05-23 19:03:47

变化

代码语言:javascript
复制
public class LinqHelper

代码语言:javascript
复制
public static class LinqHelper

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

  1. 定义扩展方法的类必须是non-genericstaticstatic扩展方法必须是
  2. 扩展方法的第一个参数应使用this关键字。
票数 348
EN

Stack Overflow用户

发布于 2018-09-07 14:28:33

如果你不想使用静态函数,只要去掉参数中的"this“关键字即可。

票数 63
EN

Stack Overflow用户

发布于 2011-05-23 19:02:17

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

代码语言:javascript
复制
// this is a non-generic static class
public static class LinqHelper
{
}
票数 21
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6096299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档