Ответ 1
public class MyClass<T>
{
public void MyMethod(T arg, bool flag)
{
Console.WriteLine("type: MyClass<{0}>, arg: {1}, flag:{2}", typeof(T),
arg.ToString(), flag);
}
public void MyMethod(T arg)
{
Console.WriteLine("type: MyClass<{0}>, arg: {1}", typeof(T), arg.ToString());
}
}
public class GenericInvokeTest
{
static MethodInfo Resolve(Type type)
{
var name = ActionName<object>(x => (o) => x.MyMethod(o));
var genericType = typeof(MyClass<>).MakeGenericType(new[] { type });
MethodInfo genericTypeMyMethodInfo = genericType.GetMethod(name); // "MyMethod");
genericTypeMyMethodInfo = genericType.GetMethod(name, new[] { type, typeof(bool) });
return genericTypeMyMethodInfo;
}
public static void Test1()
{
Resolve(typeof(string))
.Invoke(new MyClass<string>(), new object[] { "Hello, World!", true });
// Resolve(typeof(string))
.Invoke(new MyClass<string>(), new object[] { "Hello, World!" });
}
}
Чтобы сделать его строго типизированным, вы должны упростить и использовать другой подход:
1) Получите name
действия/метода с помощью выражений...
var name = ActionName<object>(x => (o) => x.MyMethod(o));
2) Затем сделайте неизбежную часть отражения
var genericType = typeof(MyClass<>).MakeGenericType(new[] { type });
MethodInfo genericTypeMyMethodInfo = genericType.GetMethod(name); // "MyMethod");
<ч/" > Где ActionName
использует аналогичный подход, например, OnPropertyChanged(x => x.Property)
public static string ActionName<T>(Expression<Func<MyClass<T>, Action<T>>> expression)
{
return GetMemberName(expression.Body);
}
public static string GetMemberName(Expression expression)
{
switch (expression.NodeType)
{
case ExpressionType.Lambda:
var lambdaExpression = (LambdaExpression)expression;
return GetMemberName(lambdaExpression.Body);
case ExpressionType.MemberAccess:
var memberExpression = (MemberExpression)expression;
var supername = GetMemberName(memberExpression.Expression);
if (String.IsNullOrEmpty(supername))
return memberExpression.Member.Name;
return String.Concat(supername, '.', memberExpression.Member.Name);
case ExpressionType.Call:
var callExpression = (MethodCallExpression)expression;
return callExpression.Method.Name;
case ExpressionType.Convert:
var unaryExpression = (UnaryExpression)expression;
return GetMemberName(unaryExpression.Operand);
case ExpressionType.Parameter:
return String.Empty;
default:
throw new ArgumentException(
"The expression is not a member access or method call expression");
}
}