如何使用GetMethod的静态扩展方法 [英] How to use GetMethod for static extension method

查看:312
本文介绍了如何使用GetMethod的静态扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个扩展方法:

 公共静态类StringEx
{
    公共静态布尔赞(这个字符串,字符串二)
    {
        返回a.ToLower()包含(b.ToLower())。
    }
}
 

如何使用我的参数选择不当通过GetMethod的反映呢?我已经试过这没有成功(有大约静态方法除外):

  VAR像= typeof运算(StringEx).GetMethod(喜欢,新的[] {typeof运算(字符串),typeof运算(字符串)});
比较器=前pression.Call(道具一样,价值);
 

解决方案

使用这种扩展方法来获得其他的扩展方法:

 公共静态类ReflectionExtensions
{
    公共静态的IEnumerable< MethodInfo的> GetExtensionMethods(这种类型的,大会extensionsAssembly)
    {
        VAR的查询=从吨extensionsAssembly.GetTypes()
                    其中,t.IsGenericType和放大器;!&安培; !t.IsNested
                    从米t.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic可)
                    其中,m.IsDefined(typeof运算(System.Runtime.CompilerServices.ExtensionAttribute),FALSE)
                    其中m.GetParameters()[0] == .ParameterType类型
                    选择米;

        返回查询;
    }

    公共静态MethodInfo的GetExtensionMethod(这种类型的,大会extensionsAssembly,字符串名称)
    {
        返回type.GetExtensionMethods(extensionsAssembly).FirstOrDefault(M => m.Name ==名);
    }

    公共静态MethodInfo的GetExtensionMethod(这种类型的,大会extensionsAssembly,字符串名称,类型[]类型)
    {
        VAR方法=(从米type.GetExtensionMethods(extensionsAssembly)
                       其中,m.Name ==名
                       &功放;&安培; m.GetParameters()。COUNT()== types.Length + 1 // + 1,因为扩展方法的参数(这一点)
                       选择M).ToList();

        如果(!methods.Any())
        {
            返回默认(MethodInfo的);
        }

        如果(methods.Count()== 1)
        {
            返回方法。首先();
        }

        的foreach(在方法变种的MethodInfo)
        {
            变种参数= methodInfo.GetParameters();

            布尔发现= TRUE;
            对于(字节B = 0; b将types.Length; b ++)
            {
                发现= TRUE;
                如果(参数并[b] .GetType()!=类型并[b])
                {
                    发现=假;
                }
            }

            如果(找到)
            {
                返回MethodInfo的;
            }
        }

        返回默认(MethodInfo的);
    }
}
 

使用这样的:

  VAR组装= Assembly.GetExecutingAssembly(); //改变这一切组件扩展方法是

VAR MethodInfo的= typeof运算(字符串).GetExtensionMethod(组装,喜欢,新的[] {typeof运算(字符串)});
 

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

解决方案

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

这篇关于如何使用GetMethod的静态扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆