簡體   English   中英

擴展方法必須在非泛型靜態類中定義

[英]Extension methods must be defined in a non-generic static class

我收到錯誤:

擴展方法必須在非泛型靜態類中定義

在線上:

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

改變

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也沒有幫助我。

我想知道是什么導致了這個錯誤?

解決方法是在同一個文件中編寫一個新的擴展類(非嵌套)並重新構建。

認為該線程獲得了足夠多的視圖,值得傳遞我找到的(有限的)解決方案。 大多數人可能嘗試在 google-ing 之前添加“靜態”以尋求解決方案! 而且我在其他任何地方都沒有看到這種解決方法。

嘗試改變

public class LinqHelper

 public static class LinqHelper

將其更改為

public static class LinqHelper

這個編譯器錯誤讓我摸不着頭腦。 我的課程不是擴展方法,幾個月以來一直運行良好,需要保持非靜態。 我在類中包含了一個新方法:

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

我從示例中復制了該方法,但沒有注意到方法簽名中的“this”修飾符,該修飾符用於擴展方法中。 刪除它解決了問題。

擴展方法應該在靜態類中。 因此,請在靜態類中添加您的擴展方法。

所以例如它應該是這樣的

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

    }

嘗試將其更改為靜態類並返回。 當它是誤報時,這可能會解決 Visual Studio 的抱怨。

我遇到了類似的問題,我創建了一個 'foo' 文件夾並在 foo 中創建了一個“類”,然后我得到了上述錯誤。 一種解決方法是將前面提到的“靜態”添加到將是“公共靜態類 LinqHelper”的類中。

我的假設是,當您在 foo 文件夾中創建一個類時,它會將其視為擴展類,因此以下規則適用於它:

1) 每個擴展方法都必須是靜態方法

變通方法 如果您不想要靜態。 我的解決方法是直接在命名空間下創建一個類,然后將其拖到“foo”文件夾中。

我在將項目轉換為使用依賴注入時遇到了這個問題。 如果方法聲明包含“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”,則可以將這些方法移至單獨的公共靜態類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM