Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

.net - How to use GetMethod for static extension method

I've got an extension method:

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

How to reflect it properly via GetMethod with my parameters? I've tried this with no success (Got an exception about static method):

var like = typeof(StringEx).GetMethod("Like", new[] {typeof(string), typeof(string)});
comparer = Expression.Call(prop, like, value);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use this extension method to get other extension methods:

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

Use it like this:

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

var methodInfo = typeof(string).GetExtensionMethod(assembly,"Like",new[] { typeof(string)});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...