开发者

C# how to create a method that invokes a delegate to another generic method

开发者 https://www.devze.com 2023-03-04 22:56 出处:网络
I have code like this.... var x = inv.Invo开发者_如何转开发keProxy<ServiceClient, AnotherType, ReturnType>(

I have code like this....

var x = inv.Invo开发者_如何转开发keProxy<ServiceClient, AnotherType, ReturnType>(
    p => p.execute(input), guid);

What I am looking to do is to encapsulate all of the above code into a delegate including the specified types.

I then want to create another method that will literally invoke the above method. Something like this...

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

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

}

Then I need to pass func to another method that will invoke it e.g.

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

Does anyone know how this can be done?


This actually is pretty simple:

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

Execute it like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号