Note that there are some explanatory texts on larger screens.

plurals
  1. POExample : Speeding up Reflection API with delegate in .NET/C#
    primarykey
    data
    text
    <p>As is asked in this <a href="https://stackoverflow.com/questions/6430479/speeding-up-reflection-api-with-delegate-in-net-c/6430561#6430561">post</a>, I came up with an example that uses Delegate to speedup Refection in .NET/C#.</p> <p>However, I got this error when running (compilation works fine). What might be wrong?</p> <pre><code>Unhandled Exception: System.ArgumentException: type is not a subclass of Multicastdelegate at System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed) [0x00000] in &lt;filename unknown&gt;:0 at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) [0x00000] in &lt;filename unknown&gt;:0 at System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) [0x00000] in &lt;filename unknown&gt;:0 at EX.RefTest.DelegateTest () [0x00000] in &lt;filename unknown&gt;:0 at EX.RefTest.Main () [0x00000] in &lt;filename unknown&gt;:0 </code></pre> <h2>ADDED</h2> <p>This is the (working) source code thanks to the help from Jon &amp; ChaosPandion.</p> <pre><code>using System.Reflection; using System; namespace EX { public class Hello { // properties public int Valx {get; set;} public int Valy {get; set;} public Hello() { Valx = 10; Valy = 20; } public int Sum(int x, int y) { Valx = x; Valy = y; return (Valx + Valy); } } public class RefTest { static void DelegateTest() { Hello h = new Hello(); Type type = h.GetType(); MethodInfo m = type.GetMethod("Sum"); // Wrong! Delegate call = Delegate.CreateDelegate(type, m); Delegate call = Delegate.CreateDelegate(typeof(Func&lt;int, int, int&gt;), h, m); int res = (int) call.DynamicInvoke(new object[] {100, 200}); Console.WriteLine("{0}", res); // This is a direct method implementation from Jon's post, and this is much faster Func&lt;int, int, int&gt; sum = (Func&lt;int, int, int&gt;) Delegate.CreateDelegate(typeof(Func&lt;int, int, int&gt;), h, m); res = sum(100, 200); Console.WriteLine("{0}", res); } static void Main() { DelegateTest(); } } } </code></pre> <h2>ADDED2</h2> <p>Based on Jon's answer, I did some performance test to use sum 1000 time. Compared to the method of using <code>(int) call.DynamicInvoke(new object[] {100, 200});</code>, <code>Func&lt;int, int, int&gt; sum = (Func&lt;int, int, int&gt;) Delegate.CreateDelegate(typeof(Func&lt;int, int, int&gt;), h, m);</code> is 300 times faster. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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