错误"扩展方法必须是静态的"是什么意思?

Tom*_*len 9 .net c# linq extension-methods compiler-errors

我在这门课上遇到了麻烦,特别是方法:

public IQueryable<T> ExecuteOrderBys<T>(this IQueryable<T> source)
Run Code Online (Sandbox Code Playgroud)

它说错误:

扩展方法必须是静态的

但是当我使方法静态时,它抛出其他区域,特别是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;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

我正在尝试创建一个可以执行以下操作的类:

var q = db.tblJobHeaders;

OrderByCollection OBys = new OrderByCollection();
OBys.AddOrderBy("some field", true);
OBys.AddOrderBy("anotherfield", false);
OBys.ExecuteOrderBys(q);
Run Code Online (Sandbox Code Playgroud)

jga*_*fin 15

你不是试图IQueryable通过你的例子的外观扩展.this从方法定义中删除,您的示例应该正常工作.

public class OrderByCollection
{
    // .. shameless cut ..
    public IQueryable<T> ExecuteOrderBys<T>(IQueryable<T> source)
    {
        // .. we don't need no stinking body ..
    }
}
Run Code Online (Sandbox Code Playgroud)

这将使您的示例工作:

var q = db.tblJobHeaders;

OrderByCollection OBys = new OrderByCollection();
OBys.AddOrderBy("some field", true);
OBys.AddOrderBy("anotherfield", false);
OBys.ExecuteOrderBys(q);
Run Code Online (Sandbox Code Playgroud)

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

OBys.AddOrderBy("some field", Sort.Ascending);
Run Code Online (Sandbox Code Playgroud)

要么:

OBys.AddOrderByDescending("some field");
Run Code Online (Sandbox Code Playgroud)

更新

扩展方法用于将"插件"方法用于现有的类或接口.通过写作public IQueryable<T> ExecuteOrderBys<T>(this IQueryable<T> source)你真的说这个方法应该被挂钩IQueryable<T>.因此应该像访问它一样访问它myQuery.ExecuteOrderBys.

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

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

b)他们可以扩展任何课程.没有限制,您可以DoSomething(this ThatObject instance)在一个名为的类中调用方法ThatObject.问题是你不能访问公共接口以外的任何东西,因为它是一个扩展方法.

令人困惑的可能性将是无止境的;)