<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>大聲想 － Think Louder &#187; CIL</title>
	<atom:link href="http://thinklouder.cn/tag/cil/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinklouder.cn</link>
	<description>on Linux, Mac, Open Source, etc.</description>
	<lastBuildDate>Mon, 05 Jul 2010 21:44:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
	<atom:link rel='hub' href='http://thinklouder.cn/?pushpress=hub'/>
		<item>
		<title>IL指令的使用</title>
		<link>http://thinklouder.cn/2009/02/il-instruction-set/</link>
		<comments>http://thinklouder.cn/2009/02/il-instruction-set/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 06:56:24 +0000</pubDate>
		<dc:creator>Sun Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[CIL]]></category>

		<guid isPermaLink="false">http://thinklouder.cn/?p=935</guid>
		<description><![CDATA[http://msdn.microsoft.com/en-us/magazine/cc301368.aspx 这篇文章介绍了MSIL大部分指令的使用。 Tags: .NET, CIL No comment]]></description>
			<content:encoded><![CDATA[<p>http://msdn.microsoft.com/en-us/magazine/cc301368.aspx</p>
<p>这篇文章介绍了MSIL大部分指令的使用。</p>
<hr />
<p>
<small>
Tags: <a href="http://thinklouder.cn/tag/net/" rel="tag">.NET</a>, <a href="http://thinklouder.cn/tag/cil/" rel="tag">CIL</a><br/>
<a href="http://thinklouder.cn/2009/02/il-instruction-set/#comments-heading">No comment</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://thinklouder.cn/2009/02/il-instruction-set/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IL中nop指令的作用</title>
		<link>http://thinklouder.cn/2009/02/purpose-of-the-nop-opcode/</link>
		<comments>http://thinklouder.cn/2009/02/purpose-of-the-nop-opcode/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 08:01:37 +0000</pubDate>
		<dc:creator>Sun Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[CIL]]></category>

		<guid isPermaLink="false">http://thinklouder.cn/?p=918</guid>
		<description><![CDATA[StackOverflow上的这个问题，引出了对nop指令作用的讨论。 http://blogs.msdn.com/oldnewthing/archive/2007/08/17/4422794.aspx http://blogs.msdn.com/rmbyers/archive/2005/09/08/462676.aspx CIL指令集 http://en.csharp-online.net/CIL_Instruction_Set 参考： Ecma-335.pdf Tags: .NET, CIL No comment]]></description>
			<content:encoded><![CDATA[<p>StackOverflow上的<a href="http://stackoverflow.com/questions/234906/whats-the-purpose-of-the-nop-opcode">这个</a>问题，引出了对nop指令作用的讨论。</p>
<p>http://blogs.msdn.com/oldnewthing/archive/2007/08/17/4422794.aspx</p>
<p>http://blogs.msdn.com/rmbyers/archive/2005/09/08/462676.aspx</p>
<p>CIL指令集</p>
<p>http://en.csharp-online.net/CIL_Instruction_Set</p>
<p>参考：<br />
Ecma-335.pdf</p>
<hr />
<p>
<small>
Tags: <a href="http://thinklouder.cn/tag/net/" rel="tag">.NET</a>, <a href="http://thinklouder.cn/tag/cil/" rel="tag">CIL</a><br/>
<a href="http://thinklouder.cn/2009/02/purpose-of-the-nop-opcode/#comments-heading">No comment</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://thinklouder.cn/2009/02/purpose-of-the-nop-opcode/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>什么是PrivateImplementationDetails</title>
		<link>http://thinklouder.cn/2009/01/what-is-privateimplementationdetails/</link>
		<comments>http://thinklouder.cn/2009/01/what-is-privateimplementationdetails/#comments</comments>
		<pubDate>Sun, 18 Jan 2009 08:42:51 +0000</pubDate>
		<dc:creator>Sun Liwen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[CIL]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[Mono]]></category>

		<guid isPermaLink="false">http://thinklouder.cn/?p=838</guid>
		<description><![CDATA[解析一个.NET程序集时发现了一段&#60;PrivateImplementationDetails&#62;{A_GUID}模样的代码，这是.NET编译器对某些指令做得优化，经分析会在如下情况下产生。 优化数组创建 var int1 = new&#91;&#93; &#123; 1, 2, 3, 4, 5, 6 &#125;; 在解析上面的代码时，发现其产生了一些由编译器控制的IL代码 IL_0008:  ldc.i4.6 IL_0009:  newarr     &#91;mscorlib&#93;System.Int32 IL_000e:  dup IL_000f:  ldtoken    field valuetype '&#60;PrivateImplementationDetails&#62;{393AE015-A550-4A61-9BFF-186AFB6A4D12}'/'__StaticArrayInitTypeSize=24' '&#60;PrivateImplementationDetails&#62;{393AE015-A550-4A61-9BFF-186AFB6A4D12}'::'$$method0x60000f8-1' IL_0014:  call       void &#91;mscorlib&#93;System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray&#40;class &#91;mscorlib&#93;System.Array, valuetype &#91;mscorlib&#93;System.RuntimeFieldHandle&#41; IL_0019:  stloc.0 如果没有使用初始化器 var int1 = new int&#91;6&#93;; 生成的IL代码如下 IL_0008: ldc.i4.6 IL_0009: newarr &#91;mscorlib&#93;System.Int32 IL_000e: stloc.0 上面是创建值类型数组的情况，如果是引用类型的数组呢？ var obj1 = [...]]]></description>
			<content:encoded><![CDATA[<p>解析一个.NET程序集时发现了一段<em>&lt;PrivateImplementationDetails&gt;{A_GUID}</em>模样的代码，这是.NET编译器对某些指令做得优化，经分析会在如下情况下产生。</p>
<h4>优化数组创建</h4>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var int1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;">1</span>, <span style="color: #FF0000;">2</span>, <span style="color: #FF0000;">3</span>, <span style="color: #FF0000;">4</span>, <span style="color: #FF0000;">5</span>, <span style="color: #FF0000;">6</span> <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>在解析上面的代码时，发现其产生了一些由编译器控制的IL代码</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">IL_0008<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>6
IL_0009<span style="color: #339933;">:</span>  newarr     <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Int32
IL_000e<span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">dup</span>
IL_000f<span style="color: #339933;">:</span>  ldtoken    field valuetype <span style="color: #7f007f;">'&lt;PrivateImplementationDetails&gt;{393AE015-A550-4A61-9BFF-186AFB6A4D12}'</span><span style="color: #339933;">/</span><span style="color: #7f007f;">'__StaticArrayInitTypeSize=24'</span> <span style="color: #7f007f;">'&lt;PrivateImplementationDetails&gt;{393AE015-A550-4A61-9BFF-186AFB6A4D12}'</span><span style="color: #339933;">::</span><span style="color: #7f007f;">'$$method0x60000f8-1'</span>
IL_0014<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       void <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Runtime<span style="color: #339933;">.</span>CompilerServices<span style="color: #339933;">.</span>RuntimeHelpers<span style="color: #339933;">::</span>InitializeArray<span style="color: #009900; font-weight: bold;">&#40;</span>class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Array<span style="color: #339933;">,</span> valuetype <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>RuntimeFieldHandle<span style="color: #009900; font-weight: bold;">&#41;</span>
IL_0019<span style="color: #339933;">:</span>  stloc<span style="color: #339933;">.</span>0</pre></div></div>

<p>如果没有使用初始化器</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var int1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">6</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span></pre></div></div>

<p>生成的IL代码如下</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">IL_0008<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>6
IL_0009<span style="color: #339933;">:</span>  newarr     <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Int32
IL_000e<span style="color: #339933;">:</span>  stloc<span style="color: #339933;">.</span>0</pre></div></div>

<p>上面是创建值类型数组的情况，如果是引用类型的数组呢？</p>
<p>
<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var obj1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">3</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
var obj2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <span style="color: #666666;">&quot;test&quot;</span>, <span style="color: #FF0000;">1</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>生成的IL代码如下</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;">IL_0001<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>3
IL_0002<span style="color: #339933;">:</span>  newarr     <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Object
IL_0007<span style="color: #339933;">:</span>  stloc<span style="color: #339933;">.</span>0
IL_0008<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>3
IL_0009<span style="color: #339933;">:</span>  newarr     <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Object
IL_000e<span style="color: #339933;">:</span>  stloc<span style="color: #339933;">.</span>2
IL_000f<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>2
IL_0010<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>0
IL_0011<span style="color: #339933;">:</span>  newobj     instance void <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Object<span style="color: #339933;">::.</span>ctor<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span>
IL_0016<span style="color: #339933;">:</span>  stelem<span style="color: #339933;">.</span>ref
IL_0017<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>2
IL_0018<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>1
IL_0019<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;test&quot;</span>
IL_001e<span style="color: #339933;">:</span>  stelem<span style="color: #339933;">.</span>ref
IL_001f<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>2
IL_0020<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>2
IL_0021<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>1
IL_0022<span style="color: #339933;">:</span>  box        <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Int32
IL_0027<span style="color: #339933;">:</span>  stelem<span style="color: #339933;">.</span>ref
IL_0028<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>2
IL_0029<span style="color: #339933;">:</span>  stloc<span style="color: #339933;">.</span>1</pre></div></div>

<p>从下图可以看到，在产生的.NET程序集中有一个&lt;PrivateImplementationDetails&gt;开头的Module</p>
<p><a href="http://thinklouder.cn/wp-content/uploads/2009/01/privateimplementationdetails.png"><img src="http://thinklouder.cn/wp-content/uploads/2009/01/privateimplementationdetails-273x300.png" alt="PrivateImplementationDetails" title="PrivateImplementationDetails" width="273" height="300" class="alignright size-medium wp-image-875" /></a></p>
<p>&lt;PrivateImplementationDetails&gt;{0BAF451E-34D3-4B3B-8567-B578D84A6965}是这个编译器产生的Module的名字，{0BAF451E-34D3-4B3B-8567-B578D84A6965}是这个dll的MVID(<a href="http://msdn.microsoft.com/en-us/library/system.reflection.module.moduleversionid.aspx">Module Version Identifier</a>)。</p>
<p>那么为什么要通过调用System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray这个静态方法来初始化数组呢？通过这位<a href="http://bartdesmet.net/blogs/bart/default.aspx">同学</a>的<a href="http://bartdesmet.net/blogs/bart/archive/2008/08/21/how-c-array-initializers-work.aspx">试验</a>可以得出结论这是为提高效率而做出的编译时优化。</p>
<p>那什么情况下.NET编译器会产生PrivateImplementationDetails这种机制呢，我还没找到一个完整的列表。除数组外我还发现大的switch表也会产生PrivateImplementationDetails，并简单的试出了启用这种机制的case数量的临界值。</p>
<h4>优化case比较多的switch</h4>
<p>在代码中加入两个方法SwitchWith6Cases()和SwitchWith7Cases()</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SwitchWith6Cases<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    var param <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;a&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>param<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;a&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;b&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;c&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;d&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;e&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;f&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SwitchWith7Cases<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    var param <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;a&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span>param<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;a&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;b&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;c&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;d&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;e&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;f&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">case</span> <span style="color: #666666;">&quot;g&quot;</span><span style="color: #008000;">:</span> break<span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>前者生成的代码如下，基本符合C#代码的逻辑</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #339933;">.</span>method <span style="color: #000000; font-weight: bold;">public</span> hidebysig instance void  SwitchWith6Cases<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span> cil managed
<span style="color: #009900; font-weight: bold;">&#123;</span>
  <span style="color: #339933;">//</span> <span style="color: #000000; font-weight: bold;">Code</span> <span style="color: #000000; font-weight: bold;">size</span>       <span style="color: #0000ff;">105</span> <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">0x69</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  <span style="color: #339933;">.</span>maxstack  <span style="color: #0000ff;">2</span>
  <span style="color: #339933;">.</span>locals init <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">0</span><span style="color: #009900; font-weight: bold;">&#93;</span> string param<span style="color: #339933;">,</span>
           <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#93;</span> string <span style="color: #00007f;">CS</span>$<span style="color: #0000ff;">4</span>$<span style="color: #0000ff;">0000</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0000<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">nop</span>
  IL_0001<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;a&quot;</span>
  IL_0006<span style="color: #339933;">:</span>  stloc<span style="color: #339933;">.</span>0
  IL_0007<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>0
  IL_0008<span style="color: #339933;">:</span>  stloc<span style="color: #339933;">.</span>1
  IL_0009<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>1
  IL_000a<span style="color: #339933;">:</span>  brfalse<span style="color: #339933;">.</span>s  IL_0068
  IL_000c<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>1
  IL_000d<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;a&quot;</span>
  IL_0012<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       bool <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>String<span style="color: #339933;">::</span>op_Equality<span style="color: #009900; font-weight: bold;">&#40;</span>string<span style="color: #339933;">,</span> string<span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0017<span style="color: #339933;">:</span>  brtrue<span style="color: #339933;">.</span>s   IL_005c
  IL_0019<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>1
  IL_001a<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;b&quot;</span>
  IL_001f<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       bool <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>String<span style="color: #339933;">::</span>op_Equality<span style="color: #009900; font-weight: bold;">&#40;</span>string<span style="color: #339933;">,</span> string<span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0024<span style="color: #339933;">:</span>  brtrue<span style="color: #339933;">.</span>s   IL_005e
  IL_0026<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>1
  IL_0027<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;c&quot;</span>
  IL_002c<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       bool <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>String<span style="color: #339933;">::</span>op_Equality<span style="color: #009900; font-weight: bold;">&#40;</span>string<span style="color: #339933;">,</span> string<span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0031<span style="color: #339933;">:</span>  brtrue<span style="color: #339933;">.</span>s   IL_0060
  IL_0033<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>1
  IL_0034<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;d&quot;</span>
  IL_0039<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       bool <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>String<span style="color: #339933;">::</span>op_Equality<span style="color: #009900; font-weight: bold;">&#40;</span>string<span style="color: #339933;">,</span> string<span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_003e<span style="color: #339933;">:</span>  brtrue<span style="color: #339933;">.</span>s   IL_0062
  IL_0040<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>1
  IL_0041<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;e&quot;</span>
  IL_0046<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       bool <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>String<span style="color: #339933;">::</span>op_Equality<span style="color: #009900; font-weight: bold;">&#40;</span>string<span style="color: #339933;">,</span> string<span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_004b<span style="color: #339933;">:</span>  brtrue<span style="color: #339933;">.</span>s   IL_0064
  IL_004d<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>1
  IL_004e<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;f&quot;</span>
  IL_0053<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       bool <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>String<span style="color: #339933;">::</span>op_Equality<span style="color: #009900; font-weight: bold;">&#40;</span>string<span style="color: #339933;">,</span> string<span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0058<span style="color: #339933;">:</span>  brtrue<span style="color: #339933;">.</span>s   IL_0066
  IL_005a<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_0068
  IL_005c<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_0068
  IL_005e<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_0068
  IL_0060<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_0068
  IL_0062<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_0068
  IL_0064<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_0068
  IL_0066<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_0068
  IL_0068<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">ret</span>
<span style="color: #009900; font-weight: bold;">&#125;</span> <span style="color: #339933;">//</span> <span style="color: #000000; font-weight: bold;">end</span> of method SwitchSample<span style="color: #339933;">::</span>SwitchWith6Cases</pre></div></div>

<p>而当switch有超过6个case的时候，所生成的IL代码如下。</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #339933;">.</span>method <span style="color: #000000; font-weight: bold;">public</span> hidebysig instance void  SwitchWith7Cases<span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#41;</span> cil managed
<span style="color: #009900; font-weight: bold;">&#123;</span>
  <span style="color: #339933;">//</span> <span style="color: #000000; font-weight: bold;">Code</span> <span style="color: #000000; font-weight: bold;">size</span>       <span style="color: #0000ff;">189</span> <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #0000ff;">0xbd</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  <span style="color: #339933;">.</span>maxstack  <span style="color: #0000ff;">4</span>
  <span style="color: #339933;">.</span>locals init <span style="color: #009900; font-weight: bold;">&#40;</span><span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">0</span><span style="color: #009900; font-weight: bold;">&#93;</span> string param<span style="color: #339933;">,</span>
           <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#93;</span> string <span style="color: #00007f;">CS</span>$<span style="color: #0000ff;">4</span>$<span style="color: #0000ff;">0000</span><span style="color: #339933;">,</span>
           <span style="color: #009900; font-weight: bold;">&#91;</span><span style="color: #0000ff;">2</span><span style="color: #009900; font-weight: bold;">&#93;</span> int32 <span style="color: #00007f;">CS</span>$<span style="color: #0000ff;">0</span>$<span style="color: #0000ff;">0001</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0000<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">nop</span>
  IL_0001<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;a&quot;</span>
  IL_0006<span style="color: #339933;">:</span>  stloc<span style="color: #339933;">.</span>0
  IL_0007<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>0
  IL_0008<span style="color: #339933;">:</span>  stloc<span style="color: #339933;">.</span>1
  IL_0009<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>1
  IL_000a<span style="color: #339933;">:</span>  brfalse    IL_00bc
  IL_000f<span style="color: #339933;">:</span>  volatile<span style="color: #339933;">.</span>
  IL_0011<span style="color: #339933;">:</span>  ldsfld     class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt; <span style="color: #7f007f;">'&lt;PrivateImplementationDetails&gt;{E6F6F304-0E96-4AD2-AF7A-7D08372A387B}'</span><span style="color: #339933;">::</span><span style="color: #7f007f;">'$$method0x60000fd-1'</span>
  IL_0016<span style="color: #339933;">:</span>  brtrue<span style="color: #339933;">.</span>s   IL_0079
  IL_0018<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>7
  IL_0019<span style="color: #339933;">:</span>  newobj     instance void class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt;<span style="color: #339933;">::.</span>ctor<span style="color: #009900; font-weight: bold;">&#40;</span>int32<span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_001e<span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">dup</span>
  IL_001f<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;a&quot;</span>
  IL_0024<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>0
  IL_0025<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       instance void class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt;<span style="color: #339933;">::</span><span style="color: #00007f; font-weight: bold;">Add</span><span style="color: #009900; font-weight: bold;">&#40;</span>!<span style="color: #0000ff;">0</span><span style="color: #339933;">,</span> !<span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_002a<span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">dup</span>
  IL_002b<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;b&quot;</span>
  IL_0030<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>1
  IL_0031<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       instance void class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt;<span style="color: #339933;">::</span><span style="color: #00007f; font-weight: bold;">Add</span><span style="color: #009900; font-weight: bold;">&#40;</span>!<span style="color: #0000ff;">0</span><span style="color: #339933;">,</span> !<span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0036<span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">dup</span>
  IL_0037<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;c&quot;</span>
  IL_003c<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>2
  IL_003d<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       instance void class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt;<span style="color: #339933;">::</span><span style="color: #00007f; font-weight: bold;">Add</span><span style="color: #009900; font-weight: bold;">&#40;</span>!<span style="color: #0000ff;">0</span><span style="color: #339933;">,</span> !<span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0042<span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">dup</span>
  IL_0043<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;d&quot;</span>
  IL_0048<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>3
  IL_0049<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       instance void class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt;<span style="color: #339933;">::</span><span style="color: #00007f; font-weight: bold;">Add</span><span style="color: #009900; font-weight: bold;">&#40;</span>!<span style="color: #0000ff;">0</span><span style="color: #339933;">,</span> !<span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_004e<span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">dup</span>
  IL_004f<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;e&quot;</span>
  IL_0054<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>4
  IL_0055<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       instance void class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt;<span style="color: #339933;">::</span><span style="color: #00007f; font-weight: bold;">Add</span><span style="color: #009900; font-weight: bold;">&#40;</span>!<span style="color: #0000ff;">0</span><span style="color: #339933;">,</span> !<span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_005a<span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">dup</span>
  IL_005b<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;f&quot;</span>
  IL_0060<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>5
  IL_0061<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       instance void class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt;<span style="color: #339933;">::</span><span style="color: #00007f; font-weight: bold;">Add</span><span style="color: #009900; font-weight: bold;">&#40;</span>!<span style="color: #0000ff;">0</span><span style="color: #339933;">,</span> !<span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0066<span style="color: #339933;">:</span>  <span style="color: #000000; font-weight: bold;">dup</span>
  IL_0067<span style="color: #339933;">:</span>  ldstr      <span style="color: #7f007f;">&quot;g&quot;</span>
  IL_006c<span style="color: #339933;">:</span>  ldc<span style="color: #339933;">.</span>i4<span style="color: #339933;">.</span>6
  IL_006d<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       instance void class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt;<span style="color: #339933;">::</span><span style="color: #00007f; font-weight: bold;">Add</span><span style="color: #009900; font-weight: bold;">&#40;</span>!<span style="color: #0000ff;">0</span><span style="color: #339933;">,</span> !<span style="color: #0000ff;">1</span><span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0072<span style="color: #339933;">:</span>  volatile<span style="color: #339933;">.</span>
  IL_0074<span style="color: #339933;">:</span>  stsfld     class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt; <span style="color: #7f007f;">'&lt;PrivateImplementationDetails&gt;{E6F6F304-0E96-4AD2-AF7A-7D08372A387B}'</span><span style="color: #339933;">::</span><span style="color: #7f007f;">'$$method0x60000fd-1'</span>
  IL_0079<span style="color: #339933;">:</span>  volatile<span style="color: #339933;">.</span>
  IL_007b<span style="color: #339933;">:</span>  ldsfld     class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt; <span style="color: #7f007f;">'&lt;PrivateImplementationDetails&gt;{E6F6F304-0E96-4AD2-AF7A-7D08372A387B}'</span><span style="color: #339933;">::</span><span style="color: #7f007f;">'$$method0x60000fd-1'</span>
  IL_0080<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>1
  IL_0081<span style="color: #339933;">:</span>  ldloca<span style="color: #339933;">.</span>s   <span style="color: #00007f;">CS</span>$<span style="color: #0000ff;">0</span>$<span style="color: #0000ff;">0001</span>
  IL_0083<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">call</span>       instance bool class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt;<span style="color: #339933;">::</span>TryGetValue<span style="color: #009900; font-weight: bold;">&#40;</span>!<span style="color: #0000ff;">0</span><span style="color: #339933;">,</span>
                                                                                                                        !<span style="color: #0000ff;">1</span>&amp;<span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_0088<span style="color: #339933;">:</span>  brfalse<span style="color: #339933;">.</span>s  IL_00bc
  IL_008a<span style="color: #339933;">:</span>  ldloc<span style="color: #339933;">.</span>2
  IL_008b<span style="color: #339933;">:</span>  switch     <span style="color: #009900; font-weight: bold;">&#40;</span> 
                        IL_00ae<span style="color: #339933;">,</span>
                        IL_00b0<span style="color: #339933;">,</span>
                        IL_00b2<span style="color: #339933;">,</span>
                        IL_00b4<span style="color: #339933;">,</span>
                        IL_00b6<span style="color: #339933;">,</span>
                        IL_00b8<span style="color: #339933;">,</span>
                        IL_00ba<span style="color: #009900; font-weight: bold;">&#41;</span>
  IL_00ac<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_00bc
  IL_00ae<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_00bc
  IL_00b0<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_00bc
  IL_00b2<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_00bc
  IL_00b4<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_00bc
  IL_00b6<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_00bc
  IL_00b8<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_00bc
  IL_00ba<span style="color: #339933;">:</span>  br<span style="color: #339933;">.</span>s       IL_00bc
  IL_00bc<span style="color: #339933;">:</span>  <span style="color: #00007f; font-weight: bold;">ret</span>
<span style="color: #009900; font-weight: bold;">&#125;</span> <span style="color: #339933;">//</span> <span style="color: #000000; font-weight: bold;">end</span> of method SwitchSample<span style="color: #339933;">::</span>SwitchWith7Cases</pre></div></div>

<p>这时候在&lt;PrivateImplementationDetails&gt;*模块中会有如下的一个叫做$$method0x60000fd-1的静态字段，辅助switch的case匹配。</p>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #339933;">.</span>field static assembly class <span style="color: #009900; font-weight: bold;">&#91;</span>mscorlib<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Collections<span style="color: #339933;">.</span>Generic<span style="color: #339933;">.</span>Dictionary`<span style="color: #0000ff;">2</span>&lt;string<span style="color: #339933;">,</span>int32&gt; <span style="color: #7f007f;">'$$method0x60000fd-1'</span></pre></div></div>

<p>通过Dictionary&lt;string,int32&gt;::TryGetValue方法去查找匹配的case。O(1)的效率在case多的情况下比线性O(n)的查找快的多。</p>
<hr />
<p>
<small>
Tags: <a href="http://thinklouder.cn/tag/net/" rel="tag">.NET</a>, <a href="http://thinklouder.cn/tag/cil/" rel="tag">CIL</a>, <a href="http://thinklouder.cn/tag/clr/" rel="tag">CLR</a>, <a href="http://thinklouder.cn/tag/mono/" rel="tag">Mono</a><br/>
<a href="http://thinklouder.cn/2009/01/what-is-privateimplementationdetails/#comments-heading">No comment</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://thinklouder.cn/2009/01/what-is-privateimplementationdetails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CIL Programming: Under the Hood of .NET</title>
		<link>http://thinklouder.cn/2008/12/cil-programming-under-the-hood-of-net/</link>
		<comments>http://thinklouder.cn/2008/12/cil-programming-under-the-hood-of-net/#comments</comments>
		<pubDate>Thu, 25 Dec 2008 04:48:26 +0000</pubDate>
		<dc:creator>Sun Liwen</dc:creator>
				<category><![CDATA[Todo]]></category>
		<category><![CDATA[Book Review]]></category>
		<category><![CDATA[CIL]]></category>

		<guid isPermaLink="false">http://thinklouder.cn/?p=621</guid>
		<description><![CDATA[在实现DTE的过程中难以避免的读或写IL，《CIL Programming》这本书给我带来很大帮助。在此记录下一些知识。 堆栈信息基础(Basic Stack Information) 四个数字类型: int32, int64, native int, F 一个对象引用类型: 0 两个指针类型: native int, &#38; 传递参数(Passing Arguments) .class SimpleType &#123; .method public instance int64 SomeMethod&#40;int32 arg1, float64 arg2, class &#91;System.Windows.Forms&#93;System.Windows.Forms.Form thisForm&#41; &#123;&#125; &#125; 这个例子中 int32 float64 class [System.Windows.Forms]System.Windows.Forms.Form 都是对参数的描述，前两个是primitive type，最后一个是引用类型。 有三个可以用来修饰参数的属性in,out,opt. in &#8211; 调用方(caller)负责将已初始化的变量作为参数传入方法。 out &#8211; 被调用方(callee)负责在方法结束之前，将参数设置成正确的类型。 opt &#8211; 表示参数是可选的，需用.param 指令(directive) Tags: Book [...]]]></description>
			<content:encoded><![CDATA[<p>在实现DTE的过程中难以避免的读或写IL，《CIL Programming》这本书给我带来很大帮助。在此记录下一些知识。</p>
<h4>堆栈信息基础(Basic Stack Information)</h4>
<p>四个数字类型: <code>int32, int64, native int, F</code><br />
一个对象引用类型: <code>0</code><br />
两个指针类型: <code>native int, &amp;</code></p>
<h4>传递参数(Passing Arguments)</h4>

<div class="wp_syntax"><div class="code"><pre class="asm" style="font-family:monospace;"><span style="color: #339933;">.</span>class SimpleType
<span style="color: #009900; font-weight: bold;">&#123;</span>
    <span style="color: #339933;">.</span>method <span style="color: #000000; font-weight: bold;">public</span> instance int64 SomeMethod<span style="color: #009900; font-weight: bold;">&#40;</span>int32 arg1<span style="color: #339933;">,</span> float64 arg2<span style="color: #339933;">,</span>
        class <span style="color: #009900; font-weight: bold;">&#91;</span>System<span style="color: #339933;">.</span>Windows<span style="color: #339933;">.</span>Forms<span style="color: #009900; font-weight: bold;">&#93;</span>System<span style="color: #339933;">.</span>Windows<span style="color: #339933;">.</span>Forms<span style="color: #339933;">.</span>Form thisForm<span style="color: #009900; font-weight: bold;">&#41;</span> <span style="color: #009900; font-weight: bold;">&#123;</span><span style="color: #009900; font-weight: bold;">&#125;</span>
<span style="color: #009900; font-weight: bold;">&#125;</span></pre></div></div>

<p>这个例子中<br />
<b>int32</b><br />
<b>float64</b><br />
<b>class [System.Windows.Forms]System.Windows.Forms.Form</b><br />
都是对参数的描述，前两个是primitive type，最后一个是引用类型。</p>
<p>有三个可以用来修饰参数的属性in,out,opt.<br />
<b>in</b> &#8211; 调用方(caller)负责将已初始化的变量作为参数传入方法。<br />
<b>out</b> &#8211; 被调用方(callee)负责在方法结束之前，将参数设置成正确的类型。<br />
<b>opt</b> &#8211; 表示参数是可选的，需用.param 指令(directive)</p>
<hr />
<p>
<small>
Tags: <a href="http://thinklouder.cn/tag/book-review/" rel="tag">Book Review</a>, <a href="http://thinklouder.cn/tag/cil/" rel="tag">CIL</a><br/>
<a href="http://thinklouder.cn/2008/12/cil-programming-under-the-hood-of-net/#comments-heading">No comment</a>
</small>
</p>]]></content:encoded>
			<wfw:commentRss>http://thinklouder.cn/2008/12/cil-programming-under-the-hood-of-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
