8

私は拡張メソッドを持っています:

public static class StringEx
{
    public static bool Like(this string a, string b)
    {
        return a.ToLower().Contains(b.ToLower());
    }
}

パラメータを使用して GetMethod を介して適切に反映するにはどうすればよいですか? 私はこれを試しましたが成功しませんでした(静的メソッドに関する例外を取得しました):

var like = typeof(StringEx).GetMethod("Like", new[] {typeof(string), typeof(string)});
comparer = Expression.Call(prop, like, value);
4

4 に答える 4

11

この拡張メソッドを使用して、他の拡張メソッドを取得します。

public static class ReflectionExtensions
{   
    public static IEnumerable<MethodInfo> GetExtensionMethods(this Type type, Assembly extensionsAssembly)
    {
        var query = from t in extensionsAssembly.GetTypes()
                    where !t.IsGenericType && !t.IsNested
                    from m in t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
                    where m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), false)
                    where m.GetParameters()[0].ParameterType == type
                    select m;

        return query;
    }

    public static MethodInfo GetExtensionMethod(this Type type, Assembly extensionsAssembly, string name)
    {
        return type.GetExtensionMethods(extensionsAssembly).FirstOrDefault(m => m.Name == name);
    }

    public static MethodInfo GetExtensionMethod(this Type type, Assembly extensionsAssembly, string name, Type[] types)
    {
        var methods = (from m in type.GetExtensionMethods(extensionsAssembly)
                       where m.Name == name
                       && m.GetParameters().Count() == types.Length + 1 // + 1 because extension method parameter (this)
                       select m).ToList();

        if (!methods.Any())
        {
            return default(MethodInfo);
        }

        if (methods.Count() == 1)
        {
            return methods.First();
        }

        foreach (var methodInfo in methods)
        {
            var parameters = methodInfo.GetParameters();

            bool found = true;
            for (byte b = 0; b < types.Length; b++)
            {
                found = true;
                if (parameters[b].GetType() != types[b])
                {
                    found = false;
                }
            }

            if (found)
            {
                return methodInfo;
            }
        }

        return default(MethodInfo);
    }
}

次のように使用します。

var assembly = Assembly.GetExecutingAssembly(); //change this to whatever assembly the extension method is in

var methodInfo = typeof(string).GetExtensionMethod(assembly,"Like",new[] { typeof(string)});
于 2013-03-10T09:35:42.820 に答える
1

GetMethodの別のオーバーロードを BindingAttr パラメーターと共に使用する必要があります。

Type extendedType = typeof(StringEx);
MethodInfo myMethodInfo = extendedType.GetMethod("Like", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic , null, new[] {typeof(string), typeof(string)},null);
于 2011-12-01T11:09:48.233 に答える
0

静的メソッドと同じように、このメソッドにアクセスできます。

var like = typeof(StringEx).GetMethod("Like", new[] { typeof(string), typeof(string) });
于 2011-12-01T07:53:13.110 に答える