我有一个方法和两个代表如下。它以这种方式运行。但我想使用Delegate.CreateInstance。 dx和dy的类型必须是Func<IEnumerable<Foo>>
。就像fx和fy一样。他们不能是Func<int, IEnumerable<Foo>>
。如何使用Delegate.CreateDelegate而不是Func <>来定义委托?
public class Test {
private IEnumerable<T> CreateItems<T>(int count) where T : class
{
for (int i = 0; i < count; i++)
{
yield return (T)Activator.CreateInstance(typeof(T), i.ToString());
}
}
public List<T> TestMethod<T>(int i = 1) where T : class
{
return CreateItems<T>(i).ToList();
}
public void TestRun()
{
const int Count = 5;
Func<IEnumerable<Foo>> fx =() => this.TestMethod<Foo>(Count);
Func<IEnumerable<Foo>> fy =() => this.TestMethod<Foo>();
var lfx = fx.Invoke();
var lfy = fy.Invoke();
var dx = Delegate.CreateDelegate(??);
var dy = Delegate.CreateDelegate(??);
var ldx = dx.DynamicInvoke();
var ldy = dy.DynamicInvoke();
}
}
'Func键>'该类型没有按” t甚至存在。 –
CodesInChaos
对不起,我写错了。它是Func> –
sinanakyazici
委托的实际类型是你想要创建的吗?真的不清楚你想达到什么。 –