加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – “扩展方法必须是静态”的错误是什么意思?

发布时间:2020-12-15 06:20:39 所属栏目:百科 来源:网络整理
导读:我有这个课的麻烦,特别是方法: public IQueryableT ExecuteOrderBysT(this IQueryableT source) 它说错误: Extension method must be static 但是当我使该方法为静态时,它会抛出其他区域,特别是对于this.xxx无法以静态方法访问. 对于 T的返回类型,我有些困
我有这个课的麻烦,特别是方法:
public IQueryable<T> ExecuteOrderBys<T>(this IQueryable<T> source)

它说错误:

Extension method must be static

但是当我使该方法为静态时,它会抛出其他区域,特别是对于this.xxx无法以静态方法访问.

对于< T>的返回类型,我有些困惑和返回类型,如果有人可以向我解释它,它的工作原理,我将不胜感激.

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


/// <summary>
/// A collection of order bys
/// </summary>
public class OrderByCollection
{
    private ArrayList Orderings = new ArrayList();

    public int? Skip { get; set; }
    public int? Take { get; set; }

    public OrderByCollection()
    {
        // Default skip and takes to nulls so we know if they are set or not
        this.Skip = null;
        this.Take = null;
    }

    /// <summary>
    /// Add an order by to this collection
    /// </summary>
    public void AddOrderBy(string Field,bool Descending)
    {
        OrderByObj NewObj = new OrderByObj(Descending,Field);
        this.Orderings.Add(NewObj);
    }

    /// <summary>
    /// Executes the order bys
    /// </summary>
    public IQueryable<T> ExecuteOrderBys<T>(this IQueryable<T> source)
    {
        int ExecutionIndex = 0;
        foreach (OrderByObj O in this.Orderings)
        {
            if (ExecutionIndex == 0)
            {
                if (O.Descending)
                    source = LinqHelper.OrderByDescending(source,O.Field);
                else
                    source = LinqHelper.OrderBy(source,O.Field);
            }
            else
            {
                if (O.Descending)
                    source = LinqHelper.ThenByDescending((IOrderedQueryable<T>)source,O.Field);
                else
                    source = LinqHelper.ThenBy((IOrderedQueryable<T>)source,O.Field);
            }
            ExecutionIndex++;
        }

        // Skip and take
        if (this.Skip != null)
            source = source.Skip(this.Skip.Value);
        if (this.Take != null)
            source = source.Take(this.Take.Value);

        return (IOrderedQueryable<T>)source;
    }
}

编辑

我正在尝试做一个可以执行以下操作的课程:

var q = db.tblJobHeaders;

OrderByCollection OBys = new OrderByCollection();
OBys.AddOrderBy("some field",true);
OBys.AddOrderBy("anotherfield",false);
OBys.ExecuteOrderBys(q);

解决方法

你不是想通过你的例子的外观来扩展IQueryable.从方法定义中删除这个,你的例子应该可以正常工作.
public class OrderByCollection
{
    // .. shameless cut ..
    public IQueryable<T> ExecuteOrderBys<T>(IQueryable<T> source)
    {
        // .. we don't need no stinking body ..
    }
}

这将使您的示例工作:

var q = db.tblJobHeaders;

OrderByCollection OBys = new OrderByCollection();
OBys.AddOrderBy("some field",false);
OBys.ExecuteOrderBys(q);

作为附注,我不会使用bool定义如何排序orderby.代码不清楚改用枚举或不同的方法:

OBys.AddOrderBy("some field",Sort.Ascending);

要么:

OBys.AddOrderByDescending("some field");

更新

扩展方法用于“插入”方法到现有的类或接口.通过写公开的IQueryable< T> ExecuteOrderBys< T>(这个IQueryable的源代码)你真的说这个方法应该被挂接到IQueryable< T>中.因此,应该像myQuery.ExecuteOrderBys一样被访问.

我的猜测是扩展方法必须是静态的,并被包含在静态类中以避免混淆:

a)他们真的不是类或接口的成员,不能访问除公共字段/属性/方法之外的任何内容.

b)他们可以扩展任何类.没有限制,你可以在一个名为ThatObject的类中调用一个方法调用DoSomething(这个ThatObject实例).问题在于,您不能访问除公共界面以外的其他内容,因为它是一种扩展方法.

混乱的可能性将是无止境的;)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读