web-dev-qa-db-ja.com

拡張メソッドは非ジェネリック静的クラスで定義する必要があります

私はエラーを得ています:

拡張メソッドは非ジェネリック静的クラスで定義する必要があります

行に:

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;
    }
}
182
Tom Gullen

変化する

public class LinqHelper

public static class LinqHelper

拡張メソッドを作成する際には、以下の点を考慮する必要があります。

  1. 拡張メソッドを定義するクラスは、non-genericstatic、およびnon-nestedである必要があります。
  2. すべての拡張メソッドはstaticメソッドでなければなりません
  3. 拡張メソッドの最初のパラメータはthisキーワードを使うべきです。
274
crypted

クラス宣言にキーワードstaticを追加します。

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

変更してみてください

public class LinqHelper

 public static class LinqHelper
16
Nathan

に変更

public static class LinqHelper
15
Rik

Nathanのようなバグを経験している人々のための回避策:

その場しのぎのコンパイラは、このExtension Methodエラーに問題があるようです... staticを追加しても私の助けにはなりませんでした。

何が原因でバグが発生しますか?

しかし、回避策は同じファイルでも(ネストされていない)新しいExtensionクラスを作成して再構築することです。

このスレッドは十分な見解を得ているので、私が見つけた(限定的な)解決策を渡す価値があります。ほとんどの人は、解決策を見つける前に、「静的」を追加しようとしたのでしょう。そして、私はこの回避策が他で修正されるのを見ませんでした。

11
Stephan Luis

静的関数を使用したくない場合は、引数の "this"キーワードを削除してください。

10
Rohan Bhosale

拡張メソッドは静的クラスの中にあるべきです。そのため、静的クラス内に拡張メソッドを追加してください。

だから例えばそれはこのようにする必要があります

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

    }
1
Debendra Dash

静的クラスに変更して元に戻してください。それが誤検出である場合、それはビジュアルスタジオの不満を解決するかもしれません。

0
visc