c# - How to make a generic delegate EndInvoke? -
After
I have the following:
personal representative Foo1 GetFooAsync1 (Foo1 foo1); Personal Representative Foo2 GetFooAsync2 (Foo2 foo2); Personal Representative Foo3 GetFooAsync3 (Foo3 foo3); Personal Representative Foo4 GetFooAsync4 (Foo4 foo4); Private FooAsync1 foo1; Private FooAsync2 foo2; Private FooAsync3 foo3; Private FooAsync4 foo4; More lists are started, and then within a method, I do not want to catch an attempt on every and every invoke, because sometimes it throws an exception but it should not be stopped by the system, And continue with the other fuses .. And this method takes a lot of space if everyone tries it around. What is the normal way to call the call from the end? Can I return the expected results? var result1 = foo1.EndInvoke (fooIAsyncResult);
To get it in a normal way, you override an extension method EndInvoke like this: Public Static Class DelegateExtensions {public static TResult EndInvoke & LT; TDelegate, TResult & gt; (The TDelegate asyncCaller, IAsyncResult asyncResult) where TDelegate: System.Delegate {TResult Results = Default (TResult); Try {result = asyncCaller.EndInvoke (asyncResult); } Hold (Exception Pre) {LogExceptionMessageOrWhatever (ex.Message); throw; } Return results; }} However, this process will generate a compiler error. Why? The System.Delegate class is a special class that can not be used in common barriers. So you can just get rid of the obstacle, and use the reflection to apply the right process?
I think that you could, but he defeats the purpose of using generic. A better solution is to make your rep as normal, then only the representative method of targeting that representative Re-write
Public Representative TFooResult GetFooAsync & Lt; TFooResult & gt; (); Public Static Class GetFooAsyncExtensions {public static TFooResult EndInvoke & LT; TFooResult & gt; (The GetFooAsync & lt; TFooResult & gt; asyncCaller, IAsyncResult asyncResult) {TFooResult Results = Default (TFooResult); Try {result = asyncCaller.EndInvoke (asyncResult); } Hold (Exception Pre) {LogExceptionMessageOrWhatever (ex.Message); throw; } Return results; }} Now you will call endInvoke as you normally would. Framework will automatically use your version. Private zeros main () {Foo1Result foo1 = null; Var foo1Factory = New GetFooAsync & lt; Foo1Result & gt; (() = & Gt; {new Foo1Result ();}); Foo1Factory.BeginInvoke (Callback: asyncResult = & gt; {foo1 = foo1Factory.EndInvoke (asyncResult);}, @object: null); }
Comments
Post a Comment