在静态类上调用重载的泛型方法

时间:2012-05-17 11:37:00

标签: c# .net asp.net-mvc-3 reflection html-helper

我正在尝试调用InputExtensions静态类上的方法,以便为HtmlHelper类上的方法创建重载。

所以无论如何,我仍然无法调用InputExtensions类的方法。以下是我用来执行此操作的部分代码:

    Type inputExtensions = typeof(InputExtensions);
    MethodInfo methodInfo = null;
    MethodInfo genericMethod = null;
    switch (propertyViewingMode.ViewingLevel)
    {
        case 1:
            methodInfo = inputExtensions.GetMethod(methodName, new Type[] { typeof(HtmlHelper<TModel>), typeof(Expression<Func<TModel, TProperty>>), typeof(Object) });
            genericMethod = methodInfo.MakeGenericMethod(typeof(TModel), typeof(TProperty));
            result = genericMethod.Invoke(null, new object[] { helper, expression, new { @class = "readOnly", @readonly = "read-only" } }) as MvcHtmlString;
            break;
        case 2:
            methodInfo = inputExtensions.GetMethod(methodName, new Type[] { typeof(HtmlHelper<TModel>), typeof(Expression<Func<TModel, TProperty>>) });
            genericMethod = methodInfo.MakeGenericMethod(typeof(TModel), typeof(TProperty));
            result = genericMethod.Invoke(null, new object[] { helper, expression }) as MvcHtmlString;
            break;
        default:
            result = new MvcHtmlString(String.Empty);
            break;
    }

问题是methodInfo始终为null,这意味着Type.GetMethod找不到我想要调用的方法。

更新:请注意,当我没有为GetMethod方法提供Type[]数组时,它会抛出一个异常,告诉我该调用是不明确的。

更新2:这是我尝试调用的方法:

TextBoxFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, Object)

解答:

    Type inputExtensions = typeof(InputExtensions);
    IEnumerable<MethodInfo> methods = null;
    MethodInfo genericMethod = null;
    MethodInfo methodInfo = null;
    switch (propertyViewingMode.ViewingLevel)
    {
        case 1:
            methods = inputExtensions.GetMethods().Where(m => m.IsGenericMethod && m.Name.Equals(methodName))
                                                        .Where(x => x.GetParameters().Count() == 3);
            methodInfo = methods.FirstOrDefault(x => x.GetParameters()[2].ParameterType.FullName == "System.Object");
            if (methodInfo != null)
            {
                genericMethod = methodInfo.MakeGenericMethod(new[] { typeof(TModel), typeof(TProperty) });
                result = genericMethod.Invoke(null, new object[] { helper, expression, new { @class = "readOnly", @readonly = "read-only" } }) as MvcHtmlString;
            }
            break;
        case 2:
            methods = inputExtensions.GetMethods().Where(m => m.IsGenericMethod && m.Name.Equals(methodName))
                                                        .Where(x => x.GetParameters().Count() == 2);
            methodInfo = methods.FirstOrDefault();
            if (methodInfo != null)
            {
                genericMethod = methodInfo.MakeGenericMethod(new[] { typeof(TModel), typeof(TProperty) });
                result = genericMethod.Invoke(null, new object[] { helper, expression }) as MvcHtmlString;
            }
            break;
        default:
            result = new MvcHtmlString(String.Empty);
            break;
    }

2 个答案:

答案 0 :(得分:3)

不幸的是,似乎不可能...... http://blogs.msdn.com/b/yirutang/archive/2005/09/14/466280.aspx

你唯一能做的就是用给定的名称迭代所有方法并自己分析参数类型:(

答案 1 :(得分:2)

我相信你想要:

BindingFlags.Public | BindingFlags.Static

编辑:

尝试在调试模式下迭代这些方法,直到找到您要查找的方法,然后您可以检查参数以确保它们都排成一行。

typeof(A).GetMethods().Where(m => m.IsGenericMethod);