在一个静态类调用重载的泛型方法 [英] Invoke an overloaded generic method on a static class
问题描述
我试图调用上的 InputExtensions 静态类,以创造重载的的HtmlHelper
类的方法。
所以无论如何,我仍然无法调用 InputExtensions
类的方法。下面是我使用这样做code部分:
键入inputExtensions = typeof运算(InputExtensions);
MethodInfo的MethodInfo的= NULL;
MethodInfo的genericMethod = NULL;
开关(propertyViewingMode.ViewingLevel)
{
情况1:
MethodInfo的= inputExtensions.GetMethod(方法名,新类型[] {typeof运算(HtmlHelper的< TModel的>)的typeof(前pression< Func键< TModel的,TProperty>>)的typeof(对象)})
genericMethod = methodInfo.MakeGenericMethod(typeof运算(TModel的)的typeof(TProperty));
结果= genericMethod.Invoke(空,新的对象[] {帮手,前pression,新{@class =表示只读,@ReadOnly =只读}})为MvcHtmlString;
打破;
案例2:
MethodInfo的= inputExtensions.GetMethod(方法名,新类型[] {typeof运算(HtmlHelper的< TModel的>)的typeof(前pression< Func键< TModel的,TProperty>>)});
genericMethod = methodInfo.MakeGenericMethod(typeof运算(TModel的)的typeof(TProperty));
结果= genericMethod.Invoke(空,新的对象[] {帮手,前pression})为MvcHtmlString;
打破;
默认:
结果=新MvcHtmlString(的String.Empty);
打破;
}
问题是,的MethodInfo
总是空的,这意味着 Type.GetMethod
没有找到方法,我敢尝试调用。
更新:需要注意的是,当我不提供 GetMethod的
方法与键入[]
阵列,它抛出一个异常,告诉我,电话是不明确的。
更新2:这是方法我想调用:
TextBoxFor< TModel的,TProperty>(HtmlHelper的< TModel的>中前pression< Func键< TModel的,TProperty>>中的对象)
答:
键入inputExtensions = typeof运算(InputExtensions);
IEnumerable的< MethodInfo的>方法= NULL;
MethodInfo的genericMethod = NULL;
MethodInfo的MethodInfo的= NULL;
开关(propertyViewingMode.ViewingLevel)
{
情况1:
方法= inputExtensions.GetMethods()式(M => m.IsGenericMethod和放大器;&安培; m.Name.Equals(方法名))。
。凡(X => x.GetParameters()COUNT()== 3);
MethodInfo的= methods.FirstOrDefault(X => x.GetParameters()[2] .ParameterType.FullName ==System.Object的);
如果(MethodInfo的!= NULL)
{
genericMethod = methodInfo.MakeGenericMethod(新[] {typeof运算(TModel的)的typeof(TProperty)});
结果= genericMethod.Invoke(空,新的对象[] {帮手,前pression,新{@class =表示只读,@ReadOnly =只读}})为MvcHtmlString;
}
打破;
案例2:
方法= inputExtensions.GetMethods()式(M => m.IsGenericMethod和放大器;&安培; m.Name.Equals(方法名))。
。凡(X => x.GetParameters()COUNT()== 2);
MethodInfo的= methods.FirstOrDefault();
如果(MethodInfo的!= NULL)
{
genericMethod = methodInfo.MakeGenericMethod(新[] {typeof运算(TModel的)的typeof(TProperty)});
结果= genericMethod.Invoke(空,新的对象[] {帮手,前pression})为MvcHtmlString;
}
打破;
默认:
结果=新MvcHtmlString(的String.Empty);
打破;
}
我相信你想要的:
BindingFlags.Public | BindingFlags.Static
编辑:
尝试遍历在调试模式下的方法,直到你找到你要找的人,那么你可以检查PARAMS,以确保他们都排队。
的typeof(A).GetMethods()式(M => m.IsGenericMethod)。
I'm trying to invoke methods on the InputExtensions static class in order to create overloads for the methods on the HtmlHelper
class.
So anyway, I still couldn't invoke the methods of the InputExtensions
class. Here's part of the code that I'm using to do so:
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;
}
The problem is that methodInfo
is always null, which means that Type.GetMethod
is not finding the method I'm trying to invoke.
UPDATE: Note that when I do not provide the GetMethod
method with a Type[]
array, it throws an exception telling me that the call is ambiguous.
UPDATE 2: This is the method I'm trying to invoke:
TextBoxFor<TModel, TProperty>(HtmlHelper<TModel>, Expression<Func<TModel, TProperty>>, Object)
ANSWER:
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;
}
I believe you want:
BindingFlags.Public | BindingFlags.Static
Edit:
Try iterating over the methods in debug mode until you find the one you're looking for, then you can examine the params to make sure they all line up.
typeof(A).GetMethods().Where(m => m.IsGenericMethod);
这篇关于在一个静态类调用重载的泛型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!