<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx</link><description>Background I&amp;#39;ve recently been doing some optimisation work which has proved quite interesting in terms of working with reflection. My efforts porting Google&amp;#39;s Protocol Buffers are now pretty complete in terms of functionality, so I&amp;#39;ve been</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1692695</link><pubDate>Tue, 19 May 2009 03:57:26 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1692695</guid><dc:creator>Kenneth Xu</dc:creator><description>&lt;p&gt;Great post! found this when I was surprised by my own test result. You article confirm this for me.&lt;/p&gt;
&lt;p&gt;In terms of the generic part, there are two different workarounds that you get return the strong typed delegate instead of general Delegate type. For detail see &lt;a rel="nofollow" target="_new" href="http://kennethxu.blogspot.com/2009/05/strong-typed-high-performance.html"&gt;kennethxu.blogspot.com/.../strong-typed-high-performance.html&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I have created a library that provides extension methods so you can simply do this:&lt;/p&gt;
&lt;p&gt;var indexOfFunc = typeof(string).GetInstanceInvoker&amp;lt;Func&amp;lt;string, char, int&amp;gt;&amp;gt;(&amp;quot;IndexOf&amp;quot;);&lt;/p&gt;
&lt;p&gt;int index = indexOfFunc(&amp;quot;Hello&amp;quot;, &amp;#39;e&amp;#39;);&lt;/p&gt;
&lt;p&gt;Cheers!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1692695" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1673334</link><pubDate>Mon, 23 Feb 2009 14:51:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1673334</guid><dc:creator>Ajai Shankar</dc:creator><description>&lt;p&gt;Jon&lt;/p&gt;
&lt;p&gt;I recently came across this post while exploring a similar implementation.&lt;/p&gt;
&lt;p&gt;Have released a multimethod dispatcher that utilizes this technique for invoking reflected methods.&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.codeplex.com/multimethods"&gt;www.codeplex.com/multimethods&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Cheers&lt;/p&gt;
&lt;p&gt;Ajai&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1673334" width="1" height="1"&gt;</description></item><item><title>Functions as return values | keyongtech</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1662878</link><pubDate>Sun, 18 Jan 2009 16:44:16 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1662878</guid><dc:creator>Functions as return values | keyongtech</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Functions as return values | keyongtech&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1662878" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1654684</link><pubDate>Thu, 20 Nov 2008 18:02:14 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1654684</guid><dc:creator>Mikhail Ryagin</dc:creator><description>&lt;p&gt;Even if you don&amp;#39;t care about performance, consider using delegate over MethodInfo.Invoke() because delegate does not wrap exceptions in&lt;/p&gt;
&lt;p&gt;TargetInvocationException. Unwrapping exceptions from TargetInvocationException can be a real pain especially when you don&amp;#39;t want to loose the stacktrace (see &lt;a rel="nofollow" target="_new" href="http://stackoverflow.com/questions/57383/in-c-how-can-i-rethrow-innerexception-without-losing-stack-trace"&gt;stackoverflow.com/.../in-c-how-can-i-rethrow-innerexception-without-losing-stack-trace&lt;/a&gt;).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1654684" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1646796</link><pubDate>Thu, 04 Sep 2008 19:14:21 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646796</guid><dc:creator>Kelvin Li</dc:creator><description>&lt;p&gt;Jon, sorry it&amp;#39;s a bug on my side. I was testing a couple of reflection methods and passed (new object[]{new char[]{&amp;#39;a&amp;#39;}}) to the delegate instead of (new char[]{&amp;#39;a&amp;#39;}). &lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646796" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1646792</link><pubDate>Thu, 04 Sep 2008 18:38:31 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646792</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Kelvin: Could you say exactly what&amp;#39;s failing for you? It&amp;#39;s not clear what you&amp;#39;re trying to do, exactly.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s the equivalent Main method calling MagicHelper (unchanged) for TrimEnd:&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static void Main()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MethodInfo trimEnd = typeof(string).GetMethod(&amp;quot;TrimEnd&amp;quot;, new Type[]{typeof(char[])});&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Func&amp;lt;string, object, object&amp;gt; trimEndFunc = MagicMethod&amp;lt;string&amp;gt;(trimEnd);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(trimEndFunc(&amp;quot;teststring&amp;quot;, new char[]{&amp;#39;a&amp;#39;}));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(trimEndFunc(&amp;quot;teststring&amp;quot;, new char[]{&amp;#39;n&amp;#39;, &amp;#39;g&amp;#39;}));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;If you want to mail me (skeet@pobox.com) any failing code, that might work better than doing it here.&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646792" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1646788</link><pubDate>Thu, 04 Sep 2008 18:12:42 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1646788</guid><dc:creator>Kelvin Li</dc:creator><description>&lt;p&gt;well, this works as long as the parameters are not array. For example, using this method to call &amp;quot;teststring&amp;quot;.TrimEnd(new char[] {&amp;#39;a&amp;#39;}) with give an error, saying cannot convert &amp;#39;System.Object[]&amp;#39; to &amp;#39;System.Char[]&amp;#39;. Any cure for this?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1646788" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1645242</link><pubDate>Wed, 20 Aug 2008 17:34:31 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1645242</guid><dc:creator>Jorge Varas</dc:creator><description>&lt;p&gt;GREAT!!!&lt;/p&gt;
&lt;p&gt;Now it works. Here is the full example, including the Correction to the lamda and the declaration of Func:&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;using System.Reflection;&lt;/p&gt;
&lt;p&gt;using System.Text;&lt;/p&gt;
&lt;p&gt;public class Test {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public delegate W Func&amp;lt;T, V, W&amp;gt;( T arg, V val );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static void Main( ) {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MethodInfo indexOf = typeof( string ).GetMethod( &amp;quot;IndexOf&amp;quot;, new Type[ ] { typeof( char ) } );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MethodInfo getByteCount = typeof( Encoding ).GetMethod( &amp;quot;GetByteCount&amp;quot;, new Type[ ] { typeof( string ) } );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Func&amp;lt;string, object, object&amp;gt; indexOfFunc = MagicMethod&amp;lt;string&amp;gt;( indexOf );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Func&amp;lt;Encoding, object, object&amp;gt; getByteCountFunc = MagicMethod&amp;lt;Encoding&amp;gt;( getByteCount );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine( indexOfFunc( &amp;quot;Hello&amp;quot;, &amp;#39;e&amp;#39; ) );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine( getByteCountFunc( Encoding.UTF8, &amp;quot;Euro sign: \u20ac&amp;quot; ) );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.ReadKey( );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static Func&amp;lt;T, object, object&amp;gt; MagicMethod&amp;lt;T&amp;gt;( MethodInfo method ) where T: class {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// First fetch the generic form&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MethodInfo genericHelper = typeof( Test ).GetMethod( &amp;quot;MagicMethodHelper&amp;quot;,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BindingFlags.Static | BindingFlags.NonPublic );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Now supply the type arguments&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;MethodInfo constructedHelper = genericHelper.MakeGenericMethod&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;( typeof( T ), method.GetParameters( )[ 0 ].ParameterType, method.ReturnType );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Now call it. The null argument is because it&amp;#39;s a static method.&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;object ret = constructedHelper.Invoke( null, new object[ ] { method } );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Cast the result to the right kind of delegate and return it&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return ( Func&amp;lt;T, object, object&amp;gt; )ret;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static Func&amp;lt;TTarget, object, object&amp;gt; MagicMethodHelper&amp;lt;TTarget, TParam, TReturn&amp;gt;( MethodInfo method )&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;where TTarget: class {&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Convert the slow MethodInfo into a fast, strongly typed, open delegate&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Func&amp;lt;TTarget, TParam, TReturn&amp;gt; func = ( Func&amp;lt;TTarget, TParam, TReturn&amp;gt; )Delegate.CreateDelegate&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;( typeof( Func&amp;lt;TTarget, TParam, TReturn&amp;gt; ), method );&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Now create a more weakly typed delegate which will call the strongly typed one&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Func&amp;lt;TTarget, object, object&amp;gt; ret = delegate( TTarget target, object param ) { return func( target, ( TParam )param ); };&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return ret;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1645242" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1645226</link><pubDate>Wed, 20 Aug 2008 16:34:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1645226</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;@Varas: You just need to change the lambda expressions into anonymous methods. For instance change the lambda expression in the example into:&lt;/p&gt;
&lt;p&gt;Func&amp;lt;TTarget, object, object&amp;gt; ret = delegate(TTarget target, object param)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{ return func(target, (TParam) param); };&lt;/p&gt;
&lt;p&gt;Hope this helps,&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1645226" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1645222</link><pubDate>Wed, 20 Aug 2008 16:26:59 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1645222</guid><dc:creator>Jorge Varas</dc:creator><description>&lt;p&gt;Hi Jon,&lt;/p&gt;
&lt;p&gt;Great article... but I have a problem.&lt;/p&gt;
&lt;p&gt;I ned to use this idea in .Net 2.0... as in I don&amp;#39;t have access to lambda expressions. (I was surpriced at your comment to Vladimir).&lt;/p&gt;
&lt;p&gt;How would this be resolved without using lambda expressions?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1645222" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1644722</link><pubDate>Fri, 15 Aug 2008 08:00:50 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1644722</guid><dc:creator>Keith J. Farmer</dc:creator><description>&lt;p&gt;I&amp;#39;m got some code lying around where I created some strong-typed wrappers around the reflection types, to denote such things as declaring class, data type of property, etc.&lt;/p&gt;
&lt;p&gt;It was an interesting exercise.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1644722" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1645050</link><pubDate>Thu, 14 Aug 2008 21:48:36 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1645050</guid><dc:creator>Vlad</dc:creator><description>&lt;p&gt;Nice writeup. An old MSDN article by Joel Pobar (CLR PM) found here: &lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/magazine/cc163759.aspx"&gt;msdn.microsoft.com/.../cc163759.aspx&lt;/a&gt; talks about similar techniques, and also includes information about dynamically generating code through DynamicMethod to improve performance.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1645050" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1644206</link><pubDate>Mon, 11 Aug 2008 08:54:24 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1644206</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Vladimir: Yes, expression trees can make this a lot simpler. Unfortunately I&amp;#39;m targeting .NET 2.0...&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1644206" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1644203</link><pubDate>Mon, 11 Aug 2008 08:32:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1644203</guid><dc:creator>Vladimir Matveev</dc:creator><description>&lt;p&gt;Hello Jon.&lt;/p&gt;
&lt;p&gt;I think, that usage of Expressions can make solution move obvious and &amp;quot;expressive&amp;quot; :)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static void Main(string[] args)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var indexOf = typeof(string).GetMethod(&amp;quot;IndexOf&amp;quot;, new[] { typeof(char) });&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var getByteCount = typeof(Encoding).GetMethod(&amp;quot;GetByteCount&amp;quot;, new[] { typeof(string) });&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var indexOfFunc = MagicMethod&amp;lt;string&amp;gt;(indexOf);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var getByteCountFunc = MagicMethod&amp;lt;Encoding&amp;gt;(getByteCount);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(indexOfFunc(&amp;quot;Hello&amp;quot;, &amp;#39;e&amp;#39;));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(getByteCountFunc(Encoding.UTF8, &amp;quot;Euro sign: \u20ac&amp;quot;));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static Func&amp;lt;T, object, object&amp;gt; MagicMethod&amp;lt;T&amp;gt;(MethodInfo method)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var parameter = method.GetParameters().Single();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var instance = Expression.Parameter(typeof (T), &amp;quot;instance&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var argument = Expression.Parameter(typeof (object), &amp;quot;argument&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var methodCall = Expression.Call(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;instance,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;method,&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Expression.Convert(argument, parameter.ParameterType)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Expression.Lambda&amp;lt;Func&amp;lt;T, object, object&amp;gt;&amp;gt;(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Expression.Convert(methodCall, typeof (object)),&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;instance, argument&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;).Compile();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1644203" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1644105</link><pubDate>Sat, 09 Aug 2008 20:42:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1644105</guid><dc:creator>skeet</dc:creator><description>&lt;p&gt;Anders: I haven&amp;#39;t actually gone down the manual IL emission route yet, but I&amp;#39;ve certainly considered it. However, I wonder what the memory implications might be. In particular, part of the point of using &amp;quot;optimise for size&amp;quot; is to avoid creating too much code for the JITter etc. I have to admit that I don&amp;#39;t know what the memory implications of using even the technique I&amp;#39;ve got at the moment might be.&lt;/p&gt;
&lt;p&gt;To be honest I would really suggest that for almost *all* situations &amp;quot;optimise for speed&amp;quot; is far more appropriate for Protocol Buffers. I don&amp;#39;t know why it&amp;#39;s not the default :(&lt;/p&gt;
&lt;p&gt;At the moment the bottleneck isn&amp;#39;t in the reflection itself, so I probably won&amp;#39;t try IL emission in this particular case... but sooner or later I&amp;#39;m bound to want it :)&lt;/p&gt;
&lt;p&gt;Jon&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1644105" width="1" height="1"&gt;</description></item><item><title>Dynamic creation of delegates using IL</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1644103</link><pubDate>Sat, 09 Aug 2008 20:20:41 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1644103</guid><dc:creator>Anders Borum</dc:creator><description>&lt;p&gt;Hi Jon,&lt;/p&gt;
&lt;p&gt;A great article on optimization - it&amp;#39;s been a while since you&amp;#39;ve been writing on this topic.&lt;/p&gt;
&lt;p&gt;While I agree that the complexity of this implementation is quite high, it&amp;#39;s important to realize that this pattern (and related implementations) is becoming more common these days - meaning that more developers would be able to step up to the task of maintaining and evolving the code.&lt;/p&gt;
&lt;p&gt;The number of applications that can benefit from this is increasing rapidly; especially with APIs in e.g. content management applications that supports untyped lists / tables (think SharePoint and so on) where dynamic creation of fields (that are not know at compile time) requires a really fast implementation.&lt;/p&gt;
&lt;p&gt;The performance of the untyped lists (and thus their overall benefit and succes) directly relates to the performance of the underlying API.&lt;/p&gt;
&lt;p&gt;I&amp;#39;m not familiar with the implementation of the Protocol Buffers projects, but if there&amp;#39;s a requirement for calling a known method on a dynamic type, you could benefit from dynamic IL emission and cache the concrete delegate.&lt;/p&gt;
&lt;p&gt;In the CMS API I&amp;#39;m working on, one of the requirements is to do just that; call a known method (in this case a constructor) on a dynamic type (a lot of examples like this is starting to show up these days - it&amp;#39;s almost like a trend). There&amp;#39;s a lot of performance in code like this, but you have to stay sharp and document every step thoroughly.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s the part of the framework I was referring to above, namely the dynamic creation of CmsField instances (that goes into the x/y coordinate of a row/column) by means of creating a concrete delegate for each dynamic type encountered during runtime.&lt;/p&gt;
&lt;p&gt;Because of optimizations like this, all types &amp;nbsp;(not just internals) become first class citizens in terms of performance (100.000s per sec.) - I really like that way of thinking.&lt;/p&gt;
&lt;p&gt;private static Dictionary&amp;lt;string, FieldDelegate&amp;gt; _cache = new Dictionary&amp;lt;string, FieldDelegate&amp;gt;();&lt;/p&gt;
&lt;p&gt;private delegate CmsField FieldDelegate(CmsContext cms);&lt;/p&gt;
&lt;p&gt;public static CmsField GetFieldDelegate(CmsContext cms, string typeName)&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt;	if (typeName == null) { throw new ArgumentNullException(&amp;quot;typeName&amp;quot;); }&lt;/p&gt;
&lt;p&gt;	FieldDelegate result = null;&lt;/p&gt;
&lt;p&gt;	if (_cache.TryGetValue(typeName, out result)) { return result(cms); }&lt;/p&gt;
&lt;p&gt;	lock (_cache)&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		if (_cache.TryGetValue(typeName, out result)) { return result(cms); }&lt;/p&gt;
&lt;p&gt;		Type type = Type.GetType(typeName);&lt;/p&gt;
&lt;p&gt;		if (type == null) { throw new InvalidOperationException(string.Format(&amp;quot;Unable to find the type &amp;#39;{0}&amp;#39;.&amp;quot;, typeName)); }&lt;/p&gt;
&lt;p&gt;		ConstructorInfo info = type.GetConstructor(new Type[] { typeof(CmsContext) });&lt;/p&gt;
&lt;p&gt;		if (info == null) { throw new InvalidOperationException(string.Format(&amp;quot;Unable to find a public constructor with the signature &amp;#39;{0}(CmsContext)&amp;#39; for type {1}&amp;quot;, type.Name, typeName)); }&lt;/p&gt;
&lt;p&gt;		var method = new DynamicMethod(&amp;quot;&amp;quot;, typeof(CmsField), new Type[] { typeof(CmsContext) }, type);&lt;/p&gt;
&lt;p&gt;		ILGenerator il = method.GetILGenerator();&lt;/p&gt;
&lt;p&gt;		il.Emit(OpCodes.Ldarg_0);&lt;/p&gt;
&lt;p&gt;		il.Emit(OpCodes.Newobj, info);&lt;/p&gt;
&lt;p&gt;		il.Emit(OpCodes.Ret);&lt;/p&gt;
&lt;p&gt;		_cache.Add(typeName, result = (FieldDelegate) method.CreateDelegate(typeof(FieldDelegate)));&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
&lt;p&gt;	return result(cms);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;I&amp;#39;m sure you&amp;#39;ve ventured down this path before, but it would be interesting to see your take on thoughts such as this (strictly related to performance).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1644103" width="1" height="1"&gt;</description></item><item><title>re: Making reflection fly and exploring delegates</title><link>http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx#1644100</link><pubDate>Sat, 09 Aug 2008 20:05:11 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1644100</guid><dc:creator>Marc Gravell</dc:creator><description>&lt;p&gt;Good write up; I use the same trick (especially the open delegate to call an instance method) quite frequently, but with an additional micro-optimisation; by making the *caller* generic, you can avoid the cast and (potentially) box/unbox. But this is nowhere as signifcant as the difference simply from using a typed delegate (rather than just Delegate or MethodInfo).&lt;/p&gt;
&lt;p&gt;Unfortunately, having a generic caller adds another dimension and tend to involve yet more MakeGeneric[Method|Type]... but &amp;quot;every little helps&amp;quot; ;-p&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1644100" width="1" height="1"&gt;</description></item></channel></rss>