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