Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think it is a very valid exercise if nothing else. I, too, am working on an asynchronous 'delegate' handler. You can increase the flexibility, and you will learn a great deal about the async model.</p> <p>I know this <a href="https://stackoverflow.com/questions/2215712/c-expression-trees-and-invoking-a-delegate/2215829#2215829">SO question</a> contains some Linq exercise in it. However, it might give you an idea on using expression trees to make it a bit more robust. I have yet to dive into the subject and provide you with more concrete information.</p> <p>Here is a sample of a piece of my old code on publishing asynchronous methods. This is a <strong>dated</strong> exercise in learning reflection and some interesting implementations. Take it for what it's worth, but it might help you with some ideas:</p> <pre><code>public delegate void delVoidMethod(params object[] args); /// &lt;summary&gt; /// Publishes an asynchronous method to the delegate collection. /// &lt;/summary&gt; /// &lt;param name="methodOwner"&gt;Target object owning the delegated method.&lt;/param&gt; /// &lt;param name="method"&gt;The delegated method.&lt;/param&gt; /// &lt;param name="callback"&gt;The method designated as a callback delegate.&lt;/param&gt; /// &lt;param name="ptr"&gt;The delegated method's runtime handle.&lt;/param&gt; /// &lt;returns&gt;True if publishing was successful.&lt;/returns&gt; public bool PublishAsyncMethod(object target , MethodInfo method , MethodInfo callback , out IntPtr ptr) { try { ptr = method.MethodHandle.Value; delVoidMethod dMethod = (delVoidMethod)Delegate.CreateDelegate (typeof(delVoidMethod) , target , method); AsyncCallback callBack = (AsyncCallback)Delegate.CreateDelegate (typeof(AsyncCallback) , target , callback); handlers[ptr] = new DelegateStruct(dMethod , callBack); Logger.WriteLine("Delegate : {0}.{1} -&gt; {2}.{3} published." , method.DeclaringType.Name , method.Name , callback.DeclaringType.Name , callback.Name); return true; } catch (ArgumentException ArgEx) { Logger.Write(DH_ERROR , ERR_MSG , ArgEx.Source , ArgEx.InnerException , ArgEx.Message); } catch (MissingMethodException BadMethEx) { Logger.Write(DH_ERROR , ERR_MSG , BadMethEx.Source , BadMethEx.InnerException , BadMethEx.Message); } catch (MethodAccessException MethAccEx) { Logger.Write(DH_ERROR , ERR_MSG , MethAccEx.Source , MethAccEx.InnerException , MethAccEx.Message); } ptr = IntPtr.Zero; return false; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload