我有以下類(我無法改變他們,他們是不是在我的控制):如何使用反射創建基於lambda的委託?
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
是父類,我有一個匿名方法的分配方法?
您需要創建和編譯'()'表達式樹 – SLaks