【发布时间】:2017-01-23 18:01:28
【问题描述】:
我有以下课程(我无法更改它们,它们不在我的控制范围内):
public abstract class BusinessBase { }
public class A : BusinessBase { }
public class B : BusinessBase { }
public class FooOne
{
public void Foo<T>(FooDelegates.Func<T> func) where T : BusinessBase { ... }
}
public class FooTwo
{
public void Foo<T>(FooDelegates.Func<T> func) where T : BusinessBase { ... }
}
public static class FooDelegates
{
public delegate TResult Func<TResult>();
}
创建委托和调用方法非常简单:
var f1 = new FooOne();
f1.Foo(() => new A());
但是,尝试使用反射来做到这一点被证明有点复杂。我有以下似乎无法完成的功能:
public class GenericHelper
{
// Parent can be A or B or etc...
// Child is FooOne or FooTwo or etc...
public void Relate(object parent, object child)
{
var mi = child.GetType().GetMethod("Foo");
var gmi = mi.MakeGenericMethod(parent.GetType());
// This next part obviously won't compile...
var del = typeof(FooDelegates.Func<>).MakeGenericType(parent.GetType());
FooDelegates.Func<parent.GetType()> del = () => parent;
gmi.Invoke(child, new object[] { del });
}
}
如何正确生成FooDelegates.Func<T>,其中T 是父类型,并且我有一个匿名方法作为分配的方法?
【问题讨论】:
-
你需要创建和
Compile()一个表达式树
标签: c# .net reflection lambda delegates