<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://trycatch.be/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>Driven by Development : MSIL</title><link>http://trycatch.be/blogs/drivenbydevelopment/archive/tags/MSIL/default.aspx</link><description>Tags: MSIL</description><dc:language>en</dc:language><generator>CommunityServer 2008 SP2 (Build: 31104.93)</generator><item><title>MSIL Part 3: Use arguments in IL</title><link>http://trycatch.be/blogs/drivenbydevelopment/archive/2008/04/02/msil-part-3-use-arguments-in-il.aspx</link><pubDate>Wed, 02 Apr 2008 18:00:46 GMT</pubDate><guid isPermaLink="false">12bbda7a-b33b-4de2-8627-f5e32a6b90ff:488</guid><dc:creator>Geoffrey Samper</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;In &lt;a href="http://trycatch.be/blogs/drivenbydevelopment/archive/2008/03/26/msil-part-2-define-a-local-in-il.aspx"&gt;part two&lt;/a&gt; I explained how to use locals in IL today I will show you how to use a arguments in IL.  &lt;p&gt;Here is the method that we will define in IL. There is no advantage of defining a method like this in IL but for this post it&amp;#39;s sufficient. &lt;p&gt;&amp;nbsp; &lt;div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d08e0b1e-e07a-448d-b93d-34352f25591e" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;&lt;pre style="background-color:White;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;white-space:pre-wrap;word-wrap:break-word;overflow:none;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt;1&lt;/span&gt; &lt;span style="color:#0000FF;"&gt;public&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; OneArgumentInCSharp(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; date) 
&lt;/span&gt;&lt;span style="color:#008080;"&gt;2&lt;/span&gt; &lt;span style="color:#000000;"&gt;{
&lt;/span&gt;&lt;span style="color:#008080;"&gt;3&lt;/span&gt; &lt;span style="color:#000000;"&gt;            &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;return&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;It&amp;#39;s &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#000000;"&gt;+&lt;/span&gt;&lt;span style="color:#000000;"&gt; date;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;4&lt;/span&gt; &lt;span style="color:#000000;"&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;
&lt;div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fc65db33-916f-420b-a83d-440257e93018" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;width:686px;padding-top:0px;"&gt;&lt;pre style="background-color:White;overflow:none;"&gt;&lt;div&gt;&lt;span style="color:#008080;"&gt; 1&lt;/span&gt; &lt;span style="color:#000000;"&gt;DynamicMethod dm &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; DynamicMethod(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;UseArgumentInIl&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;), 
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 2&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Type[] { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;) });
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 3&lt;/span&gt; &lt;span style="color:#000000;"&gt;ILGenerator generator &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; dm.GetILGenerator();
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 4&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 5&lt;/span&gt; &lt;span style="color:#000000;"&gt;MethodInfo concatMethod &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;).GetMethod(&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;Concat&lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;,
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 6&lt;/span&gt; &lt;span style="color:#000000;"&gt;    &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;new&lt;/span&gt;&lt;span style="color:#000000;"&gt; Type[] { &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;), &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;) });
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 7&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 8&lt;/span&gt; &lt;span style="color:#000000;"&gt;generator.Emit(OpCodes.Ldstr, &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#800000;"&gt;It&amp;#39;s &lt;/span&gt;&lt;span style="color:#800000;"&gt;&amp;quot;&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
&lt;/span&gt;&lt;span style="color:#008080;"&gt; 9&lt;/span&gt; &lt;span style="color:#000000;"&gt;generator.Emit(OpCodes.Ldarg_0);
&lt;/span&gt;&lt;span style="color:#008080;"&gt;10&lt;/span&gt; &lt;span style="color:#000000;"&gt;generator.EmitCall(OpCodes.Call, concatMethod,&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;null&lt;/span&gt;&lt;span style="color:#000000;"&gt;);
&lt;/span&gt;&lt;span style="color:#008080;"&gt;11&lt;/span&gt; &lt;span style="color:#000000;"&gt;generator.Emit(OpCodes.Ret);
&lt;/span&gt;&lt;span style="color:#008080;"&gt;12&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;13&lt;/span&gt; &lt;span style="color:#000000;"&gt;Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt; func;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;14&lt;/span&gt; &lt;span style="color:#000000;"&gt;func &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; (Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;)dm.CreateDelegate(&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;typeof&lt;/span&gt;&lt;span style="color:#000000;"&gt;(Func&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;, &lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt;&amp;gt;&lt;/span&gt;&lt;span style="color:#000000;"&gt;));
&lt;/span&gt;&lt;span style="color:#008080;"&gt;15&lt;/span&gt; &lt;span style="color:#000000;"&gt;
&lt;/span&gt;&lt;span style="color:#008080;"&gt;16&lt;/span&gt; &lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;span style="color:#0000FF;"&gt;string&lt;/span&gt;&lt;span style="color:#000000;"&gt; result &lt;/span&gt;&lt;span style="color:#000000;"&gt;=&lt;/span&gt;&lt;span style="color:#000000;"&gt; func(DateTime.Now.ToLongDateString());
&lt;/span&gt;&lt;span style="color:#008080;"&gt;17&lt;/span&gt; &lt;span style="color:#000000;"&gt;Console.WriteLine(result);&lt;/span&gt;&lt;/div&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Code sample 2 is the code that generates code sample 1 at runtime here the explication what it does.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;(Line 1) &lt;/strong&gt;Create an instance of the DynamicMethod Type so that it is possible to inject IL at runtime. The DynamicMethod needs to know the signature of the method this must be specified in the constructor. In this case the return type is void and it has no parameters.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;(Line 8) &lt;/strong&gt;The Ldstr loads a string on the evaluation stack in our case this is &amp;quot;It&amp;#39;s &amp;quot;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;(Line 9) &lt;/strong&gt;To opcode to use arguments is &lt;strong&gt;Ldarg&lt;/strong&gt;.Because the method has only one parameter it&amp;#39;s possible to use the shortcut OpCode &lt;strong&gt;ldarg_0&lt;/strong&gt;. This load argument at index 0 on the evaluation stack.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;(Line 10) &lt;/strong&gt;Now the concat method has to be called to concatenates the two strings.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;(Line 14) &lt;/strong&gt;Invoke the method that is generated..&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://trycatch.be/aggbug.aspx?PostID=488" width="1" height="1"&gt;</description><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/IL/default.aspx">IL</category><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/DynamicMethod/default.aspx">DynamicMethod</category><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/Relection.Emit/default.aspx">Relection.Emit</category><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/MSIL/default.aspx">MSIL</category></item><item><title>MSIL Part 2: Define a local in IL</title><link>http://trycatch.be/blogs/drivenbydevelopment/archive/2008/03/26/msil-part-2-define-a-local-in-il.aspx</link><pubDate>Wed, 26 Mar 2008 20:05:20 GMT</pubDate><guid isPermaLink="false">12bbda7a-b33b-4de2-8627-f5e32a6b90ff:482</guid><dc:creator>Geoffrey Samper</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;In &lt;a href="http://trycatch.be/blogs/drivenbydevelopment/archive/2008/03/18/want-to-learn-il-msil-the-easy-way-and-why-part-1.aspx"&gt;part one&lt;/a&gt; I explained how to call a method from IL today I will show you how to define a local variable in IL and use it. &lt;/p&gt; &lt;p&gt;Here is the method that we will define in IL. There is no advantage of defining a method like this in IL but for this post it&amp;#39;s sufficient&lt;/p&gt; &lt;p&gt;&lt;a href="http://trycatch.be/blogs/drivenbydevelopment/WindowsLiveWriter/MSILPart2DefinealocalinIL_127D8/image_6.png"&gt;&lt;img style="border-right:0px;border-top:0px;margin:0px 0px 0px 20px;border-left:0px;border-bottom:0px;" height="91" alt="image" src="http://trycatch.be/blogs/drivenbydevelopment/WindowsLiveWriter/MSILPart2DefinealocalinIL_127D8/image_thumb_2.png" width="294" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;&lt;a href="http://trycatch.be/blogs/drivenbydevelopment/WindowsLiveWriter/MSILPart2DefinealocalinIL_127D8/image_4.png"&gt;&lt;img style="border-right:0px;border-top:0px;border-left:0px;border-bottom:0px;" height="224" alt="image" src="http://trycatch.be/blogs/drivenbydevelopment/WindowsLiveWriter/MSILPart2DefinealocalinIL_127D8/image_thumb_1.png" width="665" border="0" /&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Code sample 2 is the code that generates code sample 1 at runtime here the explication what it does.&lt;/p&gt; &lt;ol&gt; &lt;li&gt;&lt;strong&gt;(Line 1) &lt;/strong&gt;Create an instance of the DynamicMethod Type so that it is possible to inject IL at runtime. The DynamicMethod needs to know the signature of the method this must be specified in the constructor. In this case the return type is void and it has no parameters.&lt;/li&gt; &lt;li&gt;&lt;strong&gt;(Line 5) &lt;/strong&gt;We need to define a local variable this can done by using the DeclareLocal method of the ILGenerator class. It requires&amp;nbsp; the type of the local as a parameter. The local will be defined in the variable list at index 0.&lt;/li&gt; &lt;li&gt;&lt;strong&gt;(Line 7) &lt;/strong&gt;The &lt;strong&gt;ldstr &lt;/strong&gt;opcode let push a string on the evaluation stack. In this case it put &amp;quot;this variable is defined in IL&amp;quot; on the evaluation stack.&lt;/li&gt; &lt;li&gt;&lt;strong&gt;(Line 8) &lt;/strong&gt;We need to store the string from &lt;strong&gt;Line 7&lt;/strong&gt; in the local variable this is possible by the Stloc opcode. In this case we use the Stloc_0 it is a shortcut to store the result in the variable list at index 0.&lt;/li&gt; &lt;li&gt;&lt;strong&gt;(Line 9) &lt;/strong&gt;We need to load the variable back on the evaluation stack so that we can call the the method WriteLine&amp;nbsp; this can be done by using the LdLoc opcode.&lt;/li&gt; &lt;li&gt;&lt;strong&gt;(Line 10) &lt;/strong&gt;Emit an opcode so that it calls the WriteLine method.&lt;/li&gt; &lt;li&gt;&lt;strong&gt;(Line 14)&lt;/strong&gt; Invoke the method that is generated. &lt;/li&gt;&lt;/ol&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://trycatch.be/aggbug.aspx?PostID=482" width="1" height="1"&gt;</description><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/IL/default.aspx">IL</category><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/DynamicMethod/default.aspx">DynamicMethod</category><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/Relection.Emit/default.aspx">Relection.Emit</category><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/MSIL/default.aspx">MSIL</category></item><item><title>Want to learn IL (MSIL). The easy way and WHY (Part 1)?</title><link>http://trycatch.be/blogs/drivenbydevelopment/archive/2008/03/18/want-to-learn-il-msil-the-easy-way-and-why-part-1.aspx</link><pubDate>Tue, 18 Mar 2008 21:47:00 GMT</pubDate><guid isPermaLink="false">12bbda7a-b33b-4de2-8627-f5e32a6b90ff:468</guid><dc:creator>Geoffrey Samper</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;The first question is why do you need know IL if you have C# VB.NET and other languages?&amp;nbsp; My reason to learn IL is when I&amp;#39;m using Reflection it is often too slow. So I need to rely on Reflection.Emit. For the lucky developers there is a new way in .NET 3.5 and it is called &lt;a href="http://msdn2.microsoft.com/nl-nl/system.linq.expressions.lambdaexpression.compile(en-us).aspx"&gt;Lambda Expressions&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So what can we do with generating IL on the fly. &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Comparer that works for all your objects so that we can sort list of objects.&lt;/li&gt;
&lt;li&gt;A method that could clone all your objects.&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Without reflection this is not possible for writing a class that can do all these kind of stuff. A solution is using Standard reflection the drawback, it is pretty slow . Here comes &lt;a href="http://msdn2.microsoft.com/system.reflection.Emit"&gt;Reflection.Emit&lt;/a&gt; to the rescue. &lt;/p&gt;
&lt;p&gt;The Reflection.Emit has one nice feature and this is &lt;a href="http://msdn2.microsoft.com/system.reflection.emit.DynamicMethod"&gt;DynamicMethod&lt;/a&gt; this class let you generate a method at runtime. &lt;/p&gt;
&lt;p&gt;Before we can begin with generating IL we need to learn IL so &lt;a href="http://weblogs.asp.net/kennykerr/"&gt;Kenny Kerr&lt;/a&gt; wrote a series of article that explains IL.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/kennykerr/archive/2004/09/07/introduction-to-msil-part-1-hello-world.aspx"&gt;Introduction to MSIL – Part 1 – Hello World&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/kennykerr/archive/2004/09/07/introduction-to-msil-part-2-using-local-variables.aspx"&gt;Introduction to MSIL – Part 2 – Using Local Variables&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/kennykerr/archive/2004/09/09/introduction-to-msil-part-3-defining-types.aspx"&gt;Introduction to MSIL – Part 3 – Defining Types&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/kennykerr/archive/2004/09/12/introduction-to-msil-part-4-defining-type-members.aspx"&gt;Introduction to MSIL – Part 4 – Defining Type Members&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/kennykerr/archive/2004/09/15/introduction-to-msil-part-5-exception-handling.aspx"&gt;Introduction to MSIL – Part 5 – Exception Handling&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/kennykerr/archive/2004/09/23/introduction-to-msil-part-6-common-language-constructs.aspx"&gt;Introduction to MSIL – Part 6 – Common Language Constructs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/kennykerr/archive/2004/10/19/introduction-to-msil-part-7-casts-and-conversions.aspx"&gt;Introduction to MSIL – Part 7 – Casts and Conversions&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://weblogs.asp.net/kennykerr/archive/2004/12/15/316014.aspx"&gt;Introduction to MSIL – Part 8 – The for each Statement&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;Now it time for generating a really simple method at runtime using IL. The method will print Hello World .&lt;/p&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;a href="http://trycatch.be/blogs/drivenbydevelopment/WindowsLiveWriter/WanttolearnILMSIL.TheeasywayandWHYPart1_1350F/image_2.png"&gt;&lt;img style="BORDER-RIGHT:0px;BORDER-TOP:0px;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height="105" alt="image" src="http://trycatch.be/blogs/drivenbydevelopment/WindowsLiveWriter/WanttolearnILMSIL.TheeasywayandWHYPart1_1350F/image_thumb.png" width="378" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="csharpcode"&gt;&lt;pre&gt;&lt;a href="http://trycatch.be/blogs/drivenbydevelopment/WindowsLiveWriter/WanttolearnILMSIL.TheeasywayandWHYPart1_1350F/image_4.png"&gt;&lt;img style="BORDER-RIGHT:0px;BORDER-TOP:0px;BORDER-LEFT:0px;BORDER-BOTTOM:0px;" height="202" alt="image" src="http://trycatch.be/blogs/drivenbydevelopment/WindowsLiveWriter/WanttolearnILMSIL.TheeasywayandWHYPart1_1350F/image_thumb_1.png" width="712" border="0" /&gt;&lt;/a&gt; &lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Code sample 2 is the code that generates code sample 1 at runtime here the explication what it does.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;(Line 1) &lt;/strong&gt;Create an instance of the DynamicMethod Type so that it is possible to inject IL at runtime. The DynamicMethod needs to know the signture of the method this must be specified in the constructor. In this case the return type is void and it has no parameters.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;(Line 6) &lt;/strong&gt;The &lt;strong&gt;ldstr &lt;/strong&gt;opcode let push a string on the evaluation stack. In this case it put &amp;quot;Hello World&amp;quot; on the stack.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;(Line 7) &lt;/strong&gt;The &lt;strong&gt;call &lt;/strong&gt;opcode let you call a method. To call a method it needs MethodInfo. &lt;/li&gt;
&lt;li&gt;&lt;strong&gt;(Line 10) &lt;/strong&gt;Call the CreateDelegate method of the DynamicMethod Type so that it can be invoked later.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;(Line 11)&amp;nbsp; &lt;/strong&gt;Invoke the method that is generated.&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;Hope it help&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://trycatch.be/aggbug.aspx?PostID=468" width="1" height="1"&gt;</description><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/IL/default.aspx">IL</category><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/DynamicMethod/default.aspx">DynamicMethod</category><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/Relection.Emit/default.aspx">Relection.Emit</category><category domain="http://trycatch.be/blogs/drivenbydevelopment/archive/tags/MSIL/default.aspx">MSIL</category></item></channel></rss>