问题
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;
}
回答1:
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);
回答2:
Unfortunately, it seems that it is not possible... http://blogs.msdn.com/b/yirutang/archive/2005/09/14/466280.aspx
The only thing you can do is to iterate over all methods with given name and analyze argument types by yourself :(
来源:https://stackoverflow.com/questions/10634988/invoke-an-overloaded-generic-method-on-a-static-class