【问题标题】:C# how to create a method that invokes a delegate to another generic methodC#如何创建一个调用另一个泛型方法的委托的方法
【发布时间】:2011-05-11 14:48:53
【问题描述】:

我有这样的代码......

var x = inv.InvokeProxy<ServiceClient, AnotherType, ReturnType>(
    p => p.execute(input), guid);

我要做的是将上述所有代码封装到一个委托中,包括指定的类型。

然后我想创建另一个方法来调用上述方法。像这样的...

Func<a,b> func = delegate()
{

.... 1st code sample inserted here ...

}

然后我需要将 func 传递给另一个将调用它的方法,例如

protected TReturn InvokeDelegate<TReturn>(Func<> functionObject)
{
    return functionObject.Invoke();
}

有谁知道如何做到这一点?

【问题讨论】:

  • 由于Func&lt;&gt;Func&lt;a,b&gt; 之类的原因,您的意思并不是很清楚。 InvokeDelegate 有什么意义 - 为什么调用者不能自己调用​​代表?

标签: c# generics delegates lambda


【解决方案1】:

这其实很简单:

Func<TypeOfInput, Guid, TypeOfX> func = (input, guid) => 
                  inv.InvokeProxy<ServiceClient, AnotherType, ReturnType>(
                                             p => p.execute(input), guid);

这样执行:

TypeOfInput yourInput = ...;
Guid yourGuid = ...;
TypeOfX x = func(yourInput, yourGuid);

【讨论】:

    猜你喜欢
    • 2015-02-05
    • 2018-11-12
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多