<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US"><title type="html">Managed CodeGen</title><subtitle type="html">This blog is to introduce some new features about Managed CodeGen in Whidbey. We provided a new way of doing LightWeight CodeGen, added support for emitting Generics in Reflection.Emit and there is some new exciting token handle stories in Reflection going on.</subtitle><id>http://blogs.msdn.com/yirutang/atom.xml</id><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/default.aspx" /><link rel="self" type="application/atom+xml" href="http://blogs.msdn.com/yirutang/atom.xml" /><generator uri="http://communityserver.org" version="2.1.61025.2">Community Server</generator><updated>2005-05-19T11:59:00Z</updated><entry><title>A Tool (attached) to help figure out how to emit a type</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2006/06/07/621125.aspx" /><link rel="enclosure" type="application/octet-stream" length="51995" href="http://blogs.msdn.com/yirutang/attachment/621125.ashx" /><id>http://blogs.msdn.com/yirutang/archive/2006/06/07/621125.aspx</id><published>2006-06-07T23:07:00Z</published><updated>2006-06-07T23:07:00Z</updated><content type="html">&lt;P&gt;After being in Exchange for almost a year, I&amp;nbsp;tend to&amp;nbsp;forget the how to run Reflection.Emit that I was once so familiared with.&amp;nbsp; Occasionally, I&amp;nbsp;would receive emails from this blog asking how to emit this, how to emit that. Before,&amp;nbsp;the solutions came directly&amp;nbsp;from my memory, but now I need some help. The way for me to figure it out is to compile the thing I want to emit into a binary file and run it under my little tool AssemblyRoundTrip.exe with verbose output. &lt;/P&gt;
&lt;P&gt;I wrote the tool for testing purpose. I ran every assembly I can get under that tool and it will figure out for me whether we can or cannot emit the assembly. &lt;/P&gt;
&lt;P&gt;Now, I found it really helpful for myself to figure out&amp;nbsp;things should be done. I am posting the tool here and hopefully it will help you to work out some generic mystery.&lt;/P&gt;
&lt;P&gt;Here is an example, if I want to define Test&amp;lt;T&amp;gt;:List&amp;lt;T&amp;gt; type, I wrote it in C# and compile it. Then I ran&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;AssemblyRoundTrip.exe repro.dll /verbose&lt;/STRONG&gt; &lt;/P&gt;
&lt;P&gt;and here is the output:&lt;/P&gt;
&lt;P&gt;new Asm Name is repro_Emit&lt;BR&gt;Security Attribute number :0&lt;BR&gt;setting ca:[System.Runtime.CompilerServices.CompilationRelaxationsAttribute((Int&lt;BR&gt;32)8)]&lt;BR&gt;setting ca:[System.Runtime.CompilerServices.RuntimeCompatibilityAttribute(WrapNo&lt;BR&gt;nExceptionThrows = True)]&lt;BR&gt;new module name is repro_Emit.dll&lt;BR&gt;Total Types:1&lt;BR&gt;&lt;STRONG&gt;m_modBuilder.DefineType M`1&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;---- Define the type&lt;BR&gt;Adding M`1[T] to the create list&lt;BR&gt;&lt;STRONG&gt;Define Generic Parameters on M`1&lt;/STRONG&gt;&amp;nbsp; &amp;lt;---- Define the type parameter&lt;BR&gt;Adding Generic Parameter M`1[T]::T on Type M`1 to the create type list&lt;BR&gt;&lt;STRONG&gt;Found a match for :M`1::T&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp; &amp;lt;--- this step is a preparation for the type below. We are binding List`1[T] (the type below)&amp;nbsp;to T&lt;BR&gt;&lt;STRONG&gt;Set Parent for M`1 System.Collections.Generic.List`1[T]&lt;/STRONG&gt;&amp;nbsp; &amp;lt;--- set the paraent&lt;BR&gt;Found a match for :M`1::T&lt;BR&gt;Set Interface for M`1 System.Collections.Generic.IList`1[T]&lt;BR&gt;Found a match for :M`1::T&lt;BR&gt;Set Interface for M`1 System.Collections.Generic.ICollection`1[T]&lt;BR&gt;Found a match for :M`1::T&lt;BR&gt;Set Interface for M`1 System.Collections.Generic.IEnumerable`1[T]&lt;BR&gt;Set Interface for M`1 System.Collections.IEnumerable&lt;BR&gt;Set Interface for M`1 System.Collections.IList&lt;BR&gt;Set Interface for M`1 System.Collections.ICollection&lt;BR&gt;+++ Create Method : M`1[T]::.ctor&lt;BR&gt;Security Attribute for type :M`1[T] is 0&lt;BR&gt;Adding M`1[T] to bake type list&lt;BR&gt;+++ Created Type : M`1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I translate the verbose output into the following code:&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;using System;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;using System.Reflection;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;using System.Collections.Generic;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;using System.Reflection.Emit;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;public class Test&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;{&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void Main()&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; string typename = "ListProxy";&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AssemblyName asmname = new AssemblyName("mytest");&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; AssemblyBuilder asmbuild = AppDomain.CurrentDomain.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.RunAndSave);&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; ModuleBuilder moduleBuilder = asmbuild.DefineDynamicModule("mytest", "mytest.dll");&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TypeAttributes typeAttributes = TypeAttributes.Public | TypeAttributes.Class;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TypeBuilder typeBuilder = moduleBuilder.DefineType(typename, typeAttributes); &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; GenericTypeParameterBuilder[] tpbs = typeBuilder.DefineGenericParameters("T"); // defines a generic type definition&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; typeBuilder.MakeGenericType(typeof(List&amp;lt;&amp;gt;).GetGenericArguments()[0]);&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; typeBuilder.SetParent(typeof(List&amp;lt;&amp;gt;).MakeGenericType(tpbs[0])); // bind the correct base type&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; typeBuilder.CreateType(); &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; asmbuild.Save("mytest");&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;FONT face=Arial color=navy size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: navy; FONT-FAMILY: Arial"&gt;}&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;I attached the tool as it is and I am &lt;STRONG&gt;unlikely&lt;/STRONG&gt; to support this tool. I could send the source code upon request but the source code is a little confusing since it is a test tool so it tend to has many extra stuff that we are interested to test but not related to the main purpose.&lt;/P&gt;
&lt;P&gt;If you set EXT_ROOT to your runtime install path, the tool will run peverify for you at the end to verify the generated assembly.&lt;/P&gt;
&lt;P&gt;You will find there is another AssemblyPrinter.exe, it is a reflection verstion of ildasm outputing text file. The only difference is that AssemblyPrinter.exe prints out members in deterministic order. So if you want to compare the two assemblies, AssemblyPrinter.exe is a good tool for that. ildasm.exe output on two assemblies compiled from the same source tend to be difference because the runtime doesn't grarantee the ordering of members to be the same. (This should remind you not to use GetMethods[1] or GetCustomAttributes[2] since they tend to change!)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=621125" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>I am moving onto Exchange Team</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/12/09/502152.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/12/09/502152.aspx</id><published>2005-12-09T23:22:00Z</published><updated>2005-12-09T23:22:00Z</updated><content type="html">&lt;P&gt;I am&amp;nbsp;going&amp;nbsp;to program in the&amp;nbsp;Server world&amp;nbsp;using managed code! Maybe after some time, I will start a new blog for IT professionals about Clustering in Exchange. But currently this blog is not likely to have new contents about the most updated CLR technology.&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;
&lt;P&gt;Yiru&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=502152" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Sample Code to invoke managed code inside profiling API</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/12/09/502143.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/12/09/502143.aspx</id><published>2005-12-09T23:01:00Z</published><updated>2005-12-09T23:01:00Z</updated><content type="html">&lt;P&gt;ProfilerCallback.cpp&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;//&amp;nbsp;This is the function&amp;nbsp;that will invoke the managed code through COM interop on another thread&lt;/P&gt;
&lt;P&gt;// this function creates the CCW object&lt;/P&gt;
&lt;P&gt;// [in] this pointer&lt;BR&gt;DWORD WINAPI CreateManagedStub(LPVOID lpParam)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; _ManagedStub * pIManagedStub = NULL;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HRESULT hr = CoCreateInstance(CLSID_ManagedStub, NULL, CLSCTX_INPROC_SERVER, IID__ManagedStub, (void **)&amp;amp;pIManagedStub);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (FAILED(hr))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("Fail to CoCreateInstance on ManagedStub class 0x%x\n", hr);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (pIManagedStub == NULL)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("pIManagedStub is null 0x%x\n", hr);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // we have the managed instance now.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((CProfilerCallback*)lpParam)-&amp;gt;m_pIManagedStub = pIManagedStub;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;&lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;//&amp;nbsp;this function is the actual caller to the managed world.&amp;nbsp;&lt;BR&gt;// this function can be used to get a managed dynamic method token back&lt;BR&gt;// [in] this pointer; method token; assembly name&lt;BR&gt;// [out] delegate type name&lt;BR&gt;DWORD WINAPI ManagedPreStub(LPVOID lpParam)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PMYDATA pData = (PMYDATA)lpParam;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LPWSTR delegateName = NULL;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (pData-&amp;gt;thisObj-&amp;gt;m_pIManagedStub == NULL)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("pData-&amp;gt;thisObj-&amp;gt;m_pIManagedStub is null");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HRESULT hr = pData-&amp;gt;thisObj-&amp;gt;m_pIManagedStub-&amp;gt;PreStub(pData-&amp;gt;assemblyName, pData-&amp;gt;methodTk, &amp;amp;delegateName);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (FAILED(hr))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("Fail to Call PreStub 0x%x\n", hr);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; } &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; wcscpy_s(pData-&amp;gt;delegateName, (wchar_t*)delegateName);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;}&lt;/P&gt;
&lt;P&gt;......&lt;/P&gt;
&lt;P&gt;// Profiler callback function&lt;/P&gt;
&lt;P&gt;HRESULT CProfilerCallback::JITCompilationStarted(UINT functionId,&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; BOOL fIsSafeToBlock)&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HANDLE hThread = NULL;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DWORD dwThreadId;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /////////////////////////////////////////////////////////&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;We dont want to continue on JITCompilationStarted if the thread is for the "managed code" we are to call from&amp;nbsp;this callback.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // We store these thread ids in our skip thread hashtable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // 1. if the JITCompilationStarted is on one of the skipping thread, avoid following steps.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DWORD currentThreadId = GetCurrentThreadId();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (m_SkipJCHashTable-&amp;gt;PLookup(currentThreadId))&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return S_OK;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // create the ManagedStub object&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (m_pIManagedStub==NULL) // if it is already created, no need to create twice&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CreateManagedStub, this, 0, &amp;amp;dwThreadId);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// add the thread Id to skip pool so that JITCompilationStarted will&amp;nbsp;do nothing on that thread.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_SkipJCHashTable-&amp;gt;SetAt(dwThreadId, 0);&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (hThread != NULL)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WaitForSingleObject(hThread, INFINITE);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CloseHandle(hThread);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_SkipJCHashTable-&amp;gt;RemoveKey(dwThreadId);&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ///////////////////////////////////////////////////////&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // try to define a managed dynamic method and get the dynamic method method token&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // prepare the parameters to be passed in&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; PMYDATA pData = (PMYDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(MYDATA));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if( pData == NULL )&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; goto exit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pData-&amp;gt;assemblyName = wszAssemblyName;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pData-&amp;gt;methodTk = tkMethod;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; pData-&amp;gt;thisObj = this;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ProfilerPrintf("Working on asm %ws func %ws.", wszAssemblyName, wszFunctionName);&lt;BR&gt;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; hThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ManagedPreStub, pData, 0, &amp;amp;dwThreadId);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_SkipJCHashTable-&amp;gt;SetAt(dwThreadId, 0);&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (hThread != NULL)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WaitForSingleObject(hThread, INFINITE);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CloseHandle(hThread);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; m_SkipJCHashTable-&amp;gt;RemoveKey(dwThreadId);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // we free up the memory here instead of in the thread because we want some return value back.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; wchar_t wszDelegateName[512];&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (pData-&amp;gt;delegateName == NULL)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return S_OK;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // copy the return value out&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; wcsncpy_s(wszDelegateName, _countof(pData-&amp;gt;delegateName), pData-&amp;gt;delegateName, _countof(pData-&amp;gt;delegateName));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; HeapFree(GetProcessHeap(), 0, pData);&lt;/P&gt;
&lt;P&gt;...&lt;/P&gt;
&lt;P&gt;We have these definitions in ProfilerCallback.h:&lt;/P&gt;
&lt;P&gt;private:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CMap&amp;lt;DWORD, DWORD, int, int&amp;gt; * m_SkipJCHashTable;&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; Enjoy!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=502143" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>CLR Profiling API</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/09/28/475098.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/09/28/475098.aspx</id><published>2005-09-29T00:50:00Z</published><updated>2005-09-29T00:50:00Z</updated><content type="html">&lt;P&gt;I think CLR Profiling API is one of the coolest things in CLR. Here is a nice &lt;A href="http://msdn.microsoft.com/msdnmag/issues/05/01/CLRProfiler/default.aspx"&gt;article&lt;/A&gt; I just found on msdn about Whidbey Profiling APIs. Note that Everett Profiling API and Whidbey Profiling API are completely not compatible. You have to explicitly implement ICorProfilerCallback2 interface to make profiling work on Whidbey.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;Profiling provides the possibility&amp;nbsp;to do&amp;nbsp;runtime tracking, tracing and assembly rewriting. It is exactly what a tester would like to do in many aspects to test managed projects. &lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;In order to show some ideas behind this, I will introduce one of our test tools here. &lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;We implemented a Just-In-Time LCG converter tool based on Profiling API's ability to do method instrument. It was always a headache to test LCG because it is a new kind of method, completely dynamic, and touches nearly every aspect of the runtime. The only way to test it well is to leverage our existing tests which already covered nearly all aspects of CLR.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;We implemented a DynMethodConverter tool based on Reflection. The tool loads the assembly and based on the methodinfos, it converts them to the dynamic methods, linked them up and invoke on the entry point. Everything looked good at the beginning but we came to many dead problems. The tool only worked on simple, verifiable test libraries. Tests with unmanaged entry points (interop tests, hosting tests), unsafe tests (cannot pass security checks while loading) and tests with drivers will have a difficult time to be adopted under the tool. &lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;Then we looked at the possibility of solving this problem using Profiling API.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;When the profiling is turned on, the JITCompliationStarted function gets called. We took that chance to change the method body to call on a DynamicMethod delegate. One tricky part is that LCG has to be created in managed code and Profiling API cannot have any managed stack on top of it. We worked around this issue by starting another thread in the JITCompilationStarted function; invoke managed code that will create the dynamic method there. We also cached that thread id in a global hashtable, so that when that managed code hits JITCompilationStarted function again, we can quickly know that we will have to do nothing there. On the managed side, it will be very natural to use token resolution APIs to get the calling method and leverage all the Reflection structures to create a dynamic method.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;This tool can be very easily adapted to the automatic testing environment. We only need to turn the profiler on and off, register the necessary managed assembly as needed. It also solved all the previous limitations of tools based on Reflection. &lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;Hope this sample helps you when you are designing your testing tool.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=475098" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Something about ReflectionOnly Context</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/09/14/466357.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/09/14/466357.aspx</id><published>2005-09-14T21:17:00Z</published><updated>2005-09-14T21:17:00Z</updated><content type="html">&lt;P&gt;I&amp;nbsp;was reading&amp;nbsp;&lt;a href="http://blogs.msdn.com/joelpob/archive/2004/06/22/163206.aspx"&gt;Joel's blog&lt;/A&gt; about new stuff in Reflection and he mentioned ReflectionOnly context. Apart from the Generics, token handle resolutions, LCG, ReflectionOnly context (actually a loader feature) had a big impact in Reflection area in Whidbey. It reminds me to talk about ReflectionOnly context thing in detail in my blog.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;ReflectionOnly context is first of all a loader concept, and here is &lt;a href="http://blogs.msdn.com/junfeng/archive/2004/08/24/219691.aspx"&gt;Junfeng's blog entry&lt;/A&gt; explaining that aspect in detail. &lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;ReflectionOnly context's impact on Reflection is also large. Basically, Reflection can be summarized into two usages: inspect the assembly metadata and do latebind invocations. ReflectionOnly context is targeted for the first scenario *only*. &lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;All&amp;nbsp;those APIs related to execution is disabled (throws InvalidOperationException) if the assembly is loaded in a ReflectionOnly context. Those APIs are xxx.Invoke, FieldInfo.GetField, FieldInfo.SetField ... Their changes&amp;nbsp;seem to be apparent and need no explanation. &lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;There&amp;nbsp;are several more APIs that are affected&amp;nbsp;by ReflectionOnly context that do need some explanations:&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;UL type=disc&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l1 level1 lfo1; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;GetCustomAttribute: The reason we have to disable this because GetCustomAttribute returns you an Attribute and to get that, we have to run the Attribute ctor. To enable the ReflectionOnly GetCustomAttribute, we provided a new API called GetCustomAttributeData that returns a CustomAttributeData object. That object contains the constructorinfo and other raw info about the&amp;nbsp;Attribute arguments. Our new document contains detailed explanation on that type. Currently, GetCustomAttributeData performs close to GetCustomAttribute, but&amp;nbsp;it doesn’t execute&amp;nbsp;the ctor.&amp;nbsp;It is possible that in the furture, we could provide a even faster GetCustomAttributeData. &lt;o:p&gt;&lt;/o:p&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;UL type=disc&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l2 level1 lfo2; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;Handle related APIs: We disable all access to handles.&amp;nbsp;Handle&amp;nbsp;is really a runtime concept, it exists in the runtime and is used for invocation operations. By disabling handle access and usage&amp;nbsp;in ReflectionOnly context, we basically guarded ourselves against many potential areas that could hack through the system trying to do invocation in ReflectionOnly context. On the other hand, we&amp;nbsp;are able to do all the assembly inspection work without handles. All token APIs not related to Handle&amp;nbsp;function in ReflectionOnly context. In the ReflectionOnly context, it is possible to build your reflector that prints out assembly content together with the methodbody. &lt;o:p&gt;&lt;/o:p&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;UL type=disc&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l0 level1 lfo3; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;You may see there&amp;nbsp;are new APIs called GetRawConstantValue on FieldInfo, PropertyInfo and ParameterInfo(GetRawDefaultValue). The usage of&amp;nbsp;those APIs&amp;nbsp;is to get the values encoded in the metadata out, which is supported in ReflectionOnly context. Particularly, if the field constant value is&amp;nbsp;an enum,&amp;nbsp;normal GetValue will try to return a enum and fail to execute the constructor, but GetRawConstantValue will return the encoded raw integer back to you. You should use GetRaw* APIs only when you are in ReflectionOnly context.&lt;o:p&gt;&lt;/o:p&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;ReflectionOnly context is&amp;nbsp;needed&amp;nbsp;for certain scenarios to work. For example, you are developing a managed build tool such as regasm.exe that is supposed to work on all platforms. If you build the tool based on normal context, the tool will have difficulty to build a&amp;nbsp;64 bit app on&amp;nbsp;32 bit platform since we cannot load a 64 bit app on 32 bit platform in normal context.&amp;nbsp;This scenario is common for build tools&amp;nbsp;since in many places, people build their 64 bit apps using 32 bit machines. It is quite fair to recommend using ReflectionOnly context if your app is using Reflection only for&amp;nbsp;content inspection&amp;nbsp;to avoid unnecessary limitations.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P&gt;The cons of ReflectionOnly context is that you cannot do any execution if the assembly is loaded that way (but for many tools, inspect the content is all Reflection used for) and you have to build your own assembly resolver since the loader won't resolve the assembly references for you automatically. ReflectionOnly load can also be used to load some unverifiable&amp;nbsp;assemblies that normal load will fail since it skipped a lot of verification steps that normal load needed to do to ensure safe execution.&lt;o:p&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=466357" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>GetMethod limitation regarding Generics</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/09/14/466280.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/09/14/466280.aspx</id><published>2005-09-14T20:46:00Z</published><updated>2005-09-14T20:46:00Z</updated><content type="html">&lt;P&gt;I've got several &lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-language: ZH-CN; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;inquiry&lt;/SPAN&gt; emails about this issue, so I think it is a good idea to publish the problem here. &lt;/P&gt;
&lt;P&gt;Say we have:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;public class Test&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp; public void Teste&amp;lt;U&amp;gt;(U teste, int i) { Console.WriteLine(teste);&amp;nbsp;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp; public void Teste&amp;lt;U&amp;gt; (U teste) { }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Is it possible to use GetMethod to get&amp;nbsp;void Teste&amp;lt;U&amp;gt;(U teste, int i)out?&lt;/P&gt;
&lt;P&gt;Unfortunately, the answer is there is no way for now to use only GetMethod to get the method before we know the type of U.&amp;nbsp;&amp;nbsp;It is a limitation in Reflection for Whidbey. &lt;/P&gt;
&lt;P&gt;Here is a possible workaround for you, you can use GetMembers to minimize the methodinfo you have to retrieve.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;using System;&lt;BR&gt;using System.Reflection;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;public class Test&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void Teste&amp;lt;U&amp;gt;(U teste, int i) { Console.WriteLine(teste);&amp;nbsp;}&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void Teste&amp;lt;U&amp;gt; (U teste) { }&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void Main()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MemberInfo[] mis = typeof(Test).GetMember("Teste*", BindingFlags.InvokeMethod|BindingFlags.Public|BindingFlags.Instance);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (mis.Length == 0) {Console.WriteLine("No Teste Methods"); return;}&lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Type U = ((MethodInfo)mis[0]).GetGenericArguments()[0]; // assume we know the&amp;nbsp;class structure above,&amp;nbsp;for simplicity.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MethodInfo mInfo = typeof(Test).GetMethod("Teste", new Type[] { U, typeof(int) });&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (mInfo.IsGenericMethod)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mInfo = mInfo.MakeGenericMethod(typeof(string));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mInfo.Invoke(new Test(), new object[] {&amp;nbsp; "Test - calling generic method", 1 });&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=466280" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>HaiboLuo's blog</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/09/13/464902.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/09/13/464902.aspx</id><published>2005-09-13T21:06:00Z</published><updated>2005-09-13T21:06:00Z</updated><content type="html">&lt;P&gt;Our Reflection QA &lt;SPAN style="FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman'; mso-fareast-font-family: 宋体; mso-fareast-language: ZH-CN; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;definitely&lt;/SPAN&gt;&amp;nbsp;knows a lot&amp;nbsp;and he is now having a blog of his own:&lt;/P&gt;
&lt;P&gt;&lt;a href="http://blogs.msdn.com/haibo_luo"&gt;http://blogs.msdn.com/haibo_luo&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;A little advertisement, I know he is going to have something cool coming up soon. Just keep an eye on it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=464902" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Reflection.Emit is not ThreadSafe</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/09/13/464894.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/09/13/464894.aspx</id><published>2005-09-13T20:54:00Z</published><updated>2005-09-13T20:54:00Z</updated><content type="html">&lt;P&gt;Users are not supposed to&amp;nbsp;operate on&amp;nbsp;same Reflection.Emit object on multiple threads. Reflection.Emit Objects are&amp;nbsp;*NOT* thread-safe. It is certainly OK to operate on different Reflection.Emit objects on different threads.&lt;/P&gt;
&lt;P&gt;We have been discovering issues when users trying to do the first scenario and we are trying hard to make the program not to crash. :) But the correctness cannot be guaranteed. So be careful.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=464894" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Unexpected TypeLoadException during Type Creation</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/07/12/437702.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/07/12/437702.aspx</id><published>2005-07-12T02:32:00Z</published><updated>2005-07-12T02:32:00Z</updated><content type="html">&lt;P&gt;We have found some limitations in Reflection.Emit that you cannot emit types that can be compiled in C# compiler. You are unlikely to hit it in&amp;nbsp;daily emit job but if ever you met one:&lt;/P&gt;
&lt;P&gt;public class E&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public struct N1&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public E e;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public struct N2&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; public N1 n1;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; N2 n2;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;}&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;There are two partial order functions that must be satisfied for ref.emit type loading to succeed and sometimes there is way to satisfy both rules. The order rules are:&lt;/P&gt;
&lt;P&gt;1. All types of all struct fields must be loaded before the type (this one is necessary)&lt;BR&gt;2. All enclosing types of all field types must be loaded before the type (this one is not necessary but is artificially imposed by the ref.emit path through the loader)&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;For rule 1 the orders are the subset of all orders where N1 is loaded before N2 and N2 is loaded before E which leaves us with only one:&lt;/P&gt;
&lt;P&gt;N1 &amp;gt; N2 &amp;gt; E&lt;/P&gt;
&lt;P&gt;For rule 2 the partial orders are the subset of all orders where E is loaded before N2 which leaves us with 2:&lt;/P&gt;
&lt;P&gt;E &amp;gt; N1 &amp;gt; N2&lt;BR&gt;N1 &amp;gt; E &amp;gt; N2&lt;/P&gt;
&lt;P&gt;But there is no intersection of these sets and so no way to create this assembly with ref.emit.&lt;/P&gt;
&lt;P&gt;Of course,&amp;nbsp;the early bound loader doesn't have the rule 2 restriction so there is a code path that will work but it is likely too much work to move reflection emit over to that code path. So at least for Whidbey, we will have to live with such restrictions. &lt;/P&gt;
&lt;P&gt;However, it is possible that you&amp;nbsp;could try to&amp;nbsp;solve the TypeLoadException by hooking your own type resolver up to the &lt;FONT color=#000000&gt;AppDomain's TypeResolve event.&lt;/FONT&gt;&lt;BR&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=437702" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Token Handle Resolution with Generics</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/07/05/435556.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/07/05/435556.aspx</id><published>2005-07-05T05:06:00Z</published><updated>2005-07-05T05:06:00Z</updated><content type="html">&lt;P&gt;In Whidbey, apart from the token handle resolution APIs I mentioned earlier, there are some overloads making these APIs look a bit more complicated. Again, I am using APIs related to MethodInfo as an representitive example:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Token -&amp;gt; Info&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;public class Module&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;{&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;public MethodBase ResolveMethod(int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Handle-&amp;gt;Info&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;public class MethodBase&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; tab-stops: .5in center 166.85pt"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;{&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Token-&amp;gt;Handle&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt; 
&lt;P&gt;&lt;/P&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;public class ModuleHandle&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public RuntimeMethodHandle ResolveMethodHandle(int methodToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;These APIs are used for Generic related method resolution. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;EM&gt;Let's talk about the Handle-&amp;gt;Info resolution first.&lt;/EM&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;public class MethodBase&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; tab-stops: .5in center 166.85pt"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;{&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;public static MethodBase GetMethodFromHandle(RuntimeMethodHandle handle, RuntimeTypeHandle declaringType)&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;Here is a bascially concept of code sharing in Generics. Again, Joel has a nice &lt;a href="http://blogs.msdn.com/joelpob/archive/2004/11/17/259224.aspx"&gt;article&lt;/A&gt; about it and within that article there are useful resource links for further reading. We are just going to put it simple here.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;In Generics, Methods&amp;nbsp; instantiated with reference type share the same body (they are represented by the same Method Desc). &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;That is G&amp;lt;String&amp;gt;.M and G&amp;lt;Object&amp;gt;.M share the same Method Desc; C.M&amp;lt;String&amp;gt; and C.M&amp;lt;Object&amp;gt; share the same Method Desc as well. Since RuntimeMethodHandle is a pointer to the method desc, those two methods have the same RuntimeMethodHandle. How to resolve this handle back correctly? &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;The good news is that the if M is&amp;nbsp;a static method on G&amp;lt;T&amp;gt;, the runtime is able to know if it is a method for G&amp;lt;String&amp;gt; or G&amp;lt;Object&amp;gt; by some additional internal data structure. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;If M is the generic method, the runtime is also able to distinquish the two. So in those cases, the additional declaringtype info is not needed for the MethodInfo to be resolved back.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;The only case runtime will not be able to differenciate the two&amp;nbsp;methods is when the M is inside G&amp;lt;T&amp;gt; and M is a instance method. Runtime depends on the this pointer's type to figure out the declaring type of the method. In our handle resolution API, we have to tell the runtime what the declaringType is. That's when the second argument is needed.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;So here is a bit interesting thing, if M is a instance method, if you emit such code&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;new G&amp;lt;String&amp;gt;.ctor&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;call G&amp;lt;Object&amp;gt;.M&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;C&amp;lt;String&amp;gt;.M will be called because the runtime figured the declaring type of the instance method through this pointer.&amp;nbsp;Of course, runtime cannot be fooled if you pass in G&amp;lt;int&amp;gt; as the this pointer for G&amp;lt;object&amp;gt;.M, it&amp;nbsp;won't pass the JIT verification.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;However if M is a static method, Runtime is clever enough to figure out the Handle is really about the G&amp;lt;String&amp;gt;.M or G&amp;lt;Object&amp;gt;.M since no "this" pointer will be passed in.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;One thing need to be noted is that, code sharing is kind of a implementation detail of CLR, you shouldn't be building a program that relies on say G&amp;lt;String&amp;gt;.M.MethodHandle == G&amp;lt;Object&amp;gt;.M.MethodHandle although this sharing is not likely to change. But maybe one day, we decide to share more (for example, share code with instantitions of same size&amp;nbsp;value type) or share less. RuntimeMethodHandle really exposed a quite low level and raw notation of the runtime, and you should be careful when using it and use it in the right way (the way we promised to work). &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;EM&gt;Now what about tokens?&lt;/EM&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;Token is the disk represntation of all kinds of types, to learn about tokens, the best way is to write a C# program and open it in ildasm.exe.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;public class G&amp;lt;T&amp;gt;&amp;nbsp; /* type def G`1&amp;lt;T&amp;gt; 02000002*/&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;public void M(T t) /* method def M(!T t) 06000001*/&lt;BR&gt;&amp;nbsp;{}&lt;BR&gt;}&lt;BR&gt;public class C&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;public static void GM&amp;lt;T&amp;gt;(T t) /* method def GM&amp;lt;T&amp;gt;(!!T t) !!T means a different generic parameter than !!T 06000003*/&lt;BR&gt;&amp;nbsp;{}&lt;BR&gt;}&lt;BR&gt;public class Test&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;public static void Main()&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;G&amp;lt;String&amp;gt; gString = new G&amp;lt;String&amp;gt;();&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;gString.M("Hehe"); /* method ref under type spec G`1&amp;lt;string&amp;gt;::M(!0)&amp;nbsp; method ref 0a000005 type spec 1b000001*/&lt;BR&gt;&amp;nbsp; C.GM&amp;lt;String&amp;gt;("Haha"); /*method spec C::GM&amp;lt;string&amp;gt;(!!0)&amp;nbsp; 2b000001*/&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT face="Times New Roman" size=3&gt;This example illustrates some of the basic concept of different type tokens (def token, ref token, and spec token)&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;in ildasm we use ! to distinquish different type parameters, !0 means it is instantiated with whatever is in !T, 0 is the type parameter index.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;In this example G&amp;lt;T&amp;gt;.M G&amp;lt;String&amp;gt;.M, C.GM&amp;lt;String&amp;gt; are all of different tokens. However the info-&amp;gt;token API will only return you def tokens, which means:&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;G&amp;lt;Object&amp;gt;.M.MetadataToken will&amp;nbsp;return&amp;nbsp;the token for G&amp;lt;!T&amp;gt;.M&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;C.GM&amp;lt;String&amp;gt;.MetadataToken will&amp;nbsp;return the token for C.GM&amp;lt;!!T&amp;gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;However, reflection was able to resolve the spec token or ref token under a spec token. For those cases to work, however, you will need to provide the "context" where the specific token lives in. The context is the calling method's generic argument (if any) and calling method's declaring type's generic argument (if any). If there is none, you can just pass in null to the two parameters.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;The reason why we need this API can be illustrated below.&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;using System;&lt;BR&gt;using System.Reflection;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;public class G1&amp;lt;T&amp;gt;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void M()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C.M&amp;lt;T&amp;gt;();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;BR&gt;public class G2&amp;lt;V&amp;gt;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void M()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; C.M&amp;lt;V&amp;gt;();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;public class C&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void M&amp;lt;Z&amp;gt;() { }&lt;BR&gt;}&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT face="Times New Roman" size=3&gt;if you compile this program and ildasm it, you will see there is only one Method Spec which says initiate the method M with the first type argument. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT face="Times New Roman" size=3&gt;&lt;/FONT&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;MethodSpec #1 (2b000001)&lt;BR&gt;&amp;nbsp;Parent&amp;nbsp;&amp;nbsp; : 0x06000005&lt;BR&gt;&amp;nbsp;&amp;nbsp;CallCnvntn: [GENERICINST]&lt;BR&gt;&amp;nbsp;&amp;nbsp;1 Arguments&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Argument #1:&amp;nbsp; Var!0&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" size=2&gt;&lt;FONT face="Times New Roman" size=3&gt;In the context of the first call of C.M&amp;lt;T&amp;gt; the instantiation should be T and in the second call, the right argument for M&amp;lt;Z&amp;gt; is V. From the metadata itself, we don't know which method it actually is and that's why the context is needed to understand that token.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;For furthur reading about tokens, the best source is the ECMA CIL specificiation:&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;A href="http://www.ecma-international.org/publications/standards/Ecma-335.htm"&gt;http://www.ecma-international.org/publications/standards/Ecma-335.htm&lt;/A&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;Today, I am searching myself on google, however, this blog wasn't shown as the top hits. I guess it is because I didn't put my name Yiru Tang here. :) &lt;/P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=435556" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Something about Resolution APIs</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/07/02/434891.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/07/02/434891.aspx</id><published>2005-07-02T08:43:00Z</published><updated>2005-07-02T08:43:00Z</updated><content type="html">&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I haven't blogged for a long time because we were busy hitting ZBB for RTM milestone.&lt;/P&gt;
&lt;P&gt;I am not aware of something special to talk about so let's talk about one of the main new features in Whidbey -- the token handle resolution APIs.&lt;/P&gt;
&lt;P&gt;We have talked about tokens before and&amp;nbsp;now it is more time for&amp;nbsp;handles. We have RuntimeTypeHandle, RuntimeMethodHandle, RuntimeFieldHandle etc for each reflection entity. They contains a pointer to the real loaded managed image, for example, the RuntimeMethodHandle points to a MethodDesc which is used when invoking the method. In ReflectionOnly context (which disallows excecution), we disallowed the accessing to these handles simply because handles have a strong excution notion associated with them.&lt;/P&gt;
&lt;P&gt;We provided a complete set of token -&amp;gt; handle -&amp;gt; info resolution APIs in Reflection. I am listing them below just for MethodInfo (or MethodBase), for Fields and Types, the APIs are similiar.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Token -&amp;gt; Info&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;public class Module&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;{&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;public MethodBase ResolveMethod(int metadataToken);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;/SPAN&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&amp;nbsp;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Info -&amp;gt; Token&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;public class MemberInfo&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;public int MetadataToken { get; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Handle-&amp;gt;Info&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;public class MethodBase&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; tab-stops: .5in center 166.85pt"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;{&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;public static MethodBase &lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;GetMethodFromHandle(RuntimeMethodHandle handle);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;}&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Info-&amp;gt;Handle&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;public class MethodInfo&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;public RuntimeMethodHandle MethodHandle { get; }&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&lt;/SPAN&gt;}&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Token-&amp;gt;Handle&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;public class ModuleHandle&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public RuntimeMethodHandle ResolveMethodHandle(int methodToken);&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;Let's talk about non-Generics first. Stories regarding Generics&amp;nbsp;are quite complicated and worth another post. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;For non-Generics, Handle -- Info -- Token has a roughly one to one correspondence.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;I said roughly because sometimes, it could the one to one correspondence could be broken. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;For example, the identity of a MethodInfo is determined by the Handle and the ReflectedType. &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;public class A&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;{&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public void M(){}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;public class B:A&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;{}&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;MethodInfo mi1 = typeof(A).GetMethod("M");&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;MethodInfo mi2 = typeof(B).GetMethod("M");&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;The two MethodInfo has the same DeclaringType and the same RuntimeMethodHandle but they have different ReflectedType, so comparing the two of them, they won't be equal. Since their RuntimeMethodHandle are the same, invoking on them we are always invoking into the same method. So RuntimeMethodHandle really means an excution entity and MethodInfo could contain more information.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;Token is completely Module based, so the same MethodInfo must have differnet tokens regarding to different modules. Within the same module, the tokens should have one to one correspondance to the MethodInfo if there is no Generic in the picture and the method is not a vararg method.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;Why we want to provide the notion of Handles and Tokens?&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;By providing tokens, we are&amp;nbsp;expanding reflect ability from purely string based into more possible dimensions (as shown in my examples in the first post). Token&amp;nbsp;to Info resolution is faster than string-&amp;gt;info resolutions&amp;nbsp;(our PM joel had an &lt;A href="http://msdn.microsoft.com/msdnmag/issues/05/07/Reflection/default.aspx"&gt;MSDN article&lt;/A&gt; about the performance topic), and tokens occupy less space than infos. If you are to cache a lot of MethodInfos in your memory, it is going to be expensive. Instead you can get the tokens from info and cache those tokens instead. When you need to use the info, use the relative cheap token to info resolution to get the info back.&amp;nbsp;Or you can cache the handles. Handles can &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;even be used directly for invocation if you have the right type of delegate defined.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;// Get the delegate's constructor&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;FONT face="Courier New" size=2&gt;ConstructorInfo ctor = typeof(MyDelegate).GetConstructor(new Type[] {typeof(object), typeof(IntPtr)});&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;// invoke on the delegate&lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;CACall del = (CACall)ctor.Invoke(new Object[] {null/*parameters to be pushed*/, myMethodHandle.GetFunctionPointer()});&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;Of course, the call above doesn't mean to save you much invocation time and working set. But it shows you how the handles means exceution entity. In the furture, we could possibliy provide more APIs to utilize the handles directly for managed code gen, but I cannot guarantee that now.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;We provide a new type called MethodBody in Whidbey. So from a MethodInfo, you can get a MethodBody object, from the MethodBody you can get infos such as Local Variables, Exception Clauses and the IL byte array. Using the token resolution API and opcode.def file, you could easily parse the array to the re-assemble the opcodes. We could do all these in metadata API before but now we can do it through reflection APIs. In other word, you&amp;nbsp;should be able to&amp;nbsp;write a managed ildasm.exe now!&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&lt;SPAN style="mso-bidi-font-size: 12.0pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;&lt;SPAN style="FONT-SIZE: 12pt; LINE-HEIGHT: 200%; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-US; mso-ansi-language: EN-US; mso-bidi-language: AR-SA"&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=434891" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Shawn's Blog about Dynamic Assemblies and Declarative Security</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/06/15/429504.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/06/15/429504.aspx</id><published>2005-06-15T17:21:00Z</published><updated>2005-06-15T17:21:00Z</updated><content type="html">&lt;P&gt;Shawn has an excellent post about Dynamic Assemblies and Declarative Security, please check it out:&lt;/P&gt;
&lt;P&gt;&lt;a href="http://blogs.msdn.com/shawnfa/archive/2005/05.aspx"&gt;http://blogs.msdn.com/shawnfa/archive/2005/05.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=429504" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Debugging LCG</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/05/26/422373.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/05/26/422373.aspx</id><published>2005-05-26T22:10:00Z</published><updated>2005-05-26T22:10:00Z</updated><content type="html">&lt;P&gt;Maybe it is a little to early to jump into this talk but I think for a user starting to use LCG, he will hit this problem in the first place. When you are emitting a dynamic method and doesn't have a way to persist it, you can imagine that it could be hard to debug. But SoS has some build-in support for LCG that works surprisingly well (Thanks to Michael Stanton(our SOS dev lead) and Dario Russi(who created LCG)).&lt;/P&gt;
&lt;P&gt;Let me start with a program that caused LCG to throw:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;using System.Reflection;&lt;BR&gt;using System.Reflection.Emit;&lt;BR&gt;using System;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;public class Test&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static void Main()&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DynamicMethod dm = new DynamicMethod("dm", typeof(Int64), new Type[]{}, typeof(Test).Module);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ILGenerator ilgen = dm.GetILGenerator();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ilgen.EmitWriteLine("Insdie DM");&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ilgen.Emit(OpCodes.Ldc_I8, 0);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ilgen.Emit(OpCodes.Ret);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dm.CreateDelegate(typeof(D)).DynamicInvoke(null);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;BR&gt;}&lt;BR&gt;delegate Int64 D();&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;When excuting the program, we throw:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&amp;gt; System.InvalidProgramException: Common Language Runtime detected an invalid program.&lt;BR&gt;&amp;nbsp;&amp;nbsp; at dm()&lt;BR&gt;&amp;nbsp;&amp;nbsp; at D.Invoke()&lt;BR&gt;&amp;nbsp;&amp;nbsp; --- End of inner exception stack trace ---&lt;BR&gt;&amp;nbsp;&amp;nbsp; at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct&amp;amp; sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)&lt;BR&gt;&amp;nbsp;&amp;nbsp; at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig,&amp;nbsp;MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) in c:\vbl\ndp\clr\src\BCL\System\RuntimeHandles.cs:line 686&lt;BR&gt;&amp;nbsp;&amp;nbsp; at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) in c:\vbl\ndp\clr\src\BCL\System\Reflection\XXXInfos.cs:line 1472&lt;BR&gt;&amp;nbsp;&amp;nbsp; at System.Delegate.DynamicInvokeImpl(Object[] args) in c:\vbl\ndp\clr\src\BCL\System\Delegate.cs:line 102&lt;BR&gt;&amp;nbsp;&amp;nbsp; at System.Delegate.DynamicInvoke(Object[] args) in c:\vbl\ndp\clr\src\BCL\System\Delegate.cs:line 94&lt;BR&gt;&amp;nbsp;&amp;nbsp; at Test.Main()&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Welcome to the LCG world. :-) How could we debug this issue?&lt;/P&gt;
&lt;P&gt;Let's start "windbg repro.exe" and let it stop at CLR exceptions by typing in "sxe clr". (I will usually type in "sxe eh" as well just as a habbit).&lt;/P&gt;
&lt;P&gt;It will stop at the place it is throwing and the call stack looks like this:&lt;/P&gt;
&lt;P&gt;0:000&amp;gt; kb&lt;BR&gt;ChildEBP RetAddr&amp;nbsp; Args to Child&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;0012ba94 5b15cfc9 0012bcac 5b0fc1a6 00000727 mscorjit!debugError+0xc8 [c:\vbl\ndp\clr\src\jit\il\error.cpp @ 113]&lt;BR&gt;0012beac 5b16488d 5b0fc8d8 5b0fca20 00000009 mscorjit!badCode3+0x69 [c:\vbl\ndp\clr\src\jit\il\error.cpp @ 219]&lt;BR&gt;0012bf08 5b1660c1 00217031 00000006 0022774c mscorjit!Compiler::fgFindJumpTargets+0x51bd [c:\vbl\ndp\clr\src\jit\il\flowgraph.cpp @ 1834]&lt;BR&gt;0012bfa0 5b1598d2 0012d558 0012d520 00162a80 mscorjit!Compiler::fgFindBasicBlocks+0x81 [c:\vbl\ndp\clr\src\jit\il\flowgraph.cpp @ 2418]&lt;BR&gt;0012c034 5b15ac98 02e21b49 0012cfbc 0012c700 mscorjit!Compiler::compCompile+0x702 [c:\vbl\ndp\clr\src\jit\il\compiler.cpp @ 2376]&lt;BR&gt;0012c0e0 5b15be5b 0012cfbc 0012c700 0012c128 mscorjit!jitNativeCode+0x178 [c:\vbl\ndp\clr\src\jit\il\compiler.cpp @ 2845]&lt;BR&gt;0012c13c 5dd01521 5b249ed4 0012cfbc 0012c700 mscorjit!CILJit::compileMethod+0xbb [c:\vbl\ndp\clr\src\jit\il\ee_il_dll.cpp @ 212]&lt;BR&gt;0012c1f0 5dd0185d 0020e548 0012cfbc 0012c700 mscorwks!invokeCompileMethodHelper+0x101 [c:\vbl\ndp\clr\src\vm\jitinterface.cpp @ 9763]&lt;BR&gt;0012c2e8 5dd01adb 0020e548 0012cfbc 0012c700 mscorwks!invokeCompileMethod+0x17d [c:\vbl\ndp\clr\src\vm\jitinterface.cpp @ 9791]&lt;BR&gt;0012c354 5dd0288e 0020e548 0012cfbc 0012c700 mscorwks!CallCompileMethodWithSEHWrapper+0x1db [c:\vbl\ndp\clr\src\vm\jitinterface.cpp @ 9881]&lt;BR&gt;&lt;STRONG&gt;0012d144 5dfb0042 &lt;FONT color=#ff0000&gt;02e21430&lt;/FONT&gt; 00000000 00107410 mscorwks!UnsafeJitFunction+0x5de [c:\vbl\ndp\clr\src\vm\jitinterface.cpp @ 10289]&lt;/STRONG&gt;&lt;BR&gt;0012d234 5dfb2b12 00000000 00000000 0012d558 mscorwks!MethodDesc::MakeJitWorker+0x362 [c:\vbl\ndp\clr\src\vm\prestub.cpp @ 437]&lt;BR&gt;0012d3e0 5dfb1726 00000000 0012d558 0012d520 mscorwks!MethodDesc::DoPrestub+0xdd2 [c:\vbl\ndp\clr\src\vm\prestub.cpp @ 1447]&lt;BR&gt;0012d4f0 00391f0a 0012d520 12345678 5e58f190 mscorwks!PreStubWorker+0x326 [c:\vbl\ndp\clr\src\vm\prestub.cpp @ 906]&lt;BR&gt;WARNING: Frame IP not in any known module. Following frames may be wrong.&lt;BR&gt;0012d508 00ba0256 00000000 0012d558 00162a80 0x391f0a&lt;BR&gt;0012d540 5d86cf0d 0012e648 00bc46e4 00bc46e4 CLRStub[StubLinkStub]@ba0256&lt;BR&gt;0012d570 5da1710d 0012db30 00000000 0012db00 mscorwks!CallDescrWorkerInternal+0x33 [c:\vbl\ndp\clr\src\vm\i386\asmhelpers.asm @ 930]&lt;BR&gt;0012d9a4 5da16e07 0012db30 00000000 0012db00 mscorwks!CallDescrWorker+0x10d [c:\vbl\ndp\clr\src\vm\class.cpp @ 13529]&lt;BR&gt;0012dae4 5dd0e6ad 0012db30 00000000 0012db00 mscorwks!CallDescrWorkerWithHandler+0x2b7 [c:\vbl\ndp\clr\src\vm\class.cpp @ 13436]&lt;BR&gt;0012def4 5dd0d93a 02e22534 0012e22c 0012e0f0 mscorwks!MethodDesc::CallDescr+0xd1d [c:\vbl\ndp\clr\src\vm\method.cpp @ 2288]&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;We can see that JIT is throwing because we emitted some invalid IL. &lt;/P&gt;
&lt;P&gt;The first argument for UnsafeJitFunction is the Dynamic Method's MethodDesc.&lt;/P&gt;
&lt;P&gt;if we do:&lt;/P&gt;
&lt;P&gt;0:000&amp;gt; .loadby sos mscorwks&lt;BR&gt;0:000&amp;gt; !dumpmd 02e21430&lt;/P&gt;
&lt;P&gt;Method Name: DynamicClass.dm()&lt;BR&gt;Class: 02e21288&lt;BR&gt;MethodTable: 02e212f8&lt;BR&gt;mdToken: 06000000&lt;BR&gt;Module: 0020e0b0&lt;BR&gt;IsJitted: no&lt;BR&gt;m_CodeOrIL: ffffffff&lt;/P&gt;
&lt;P&gt;We can see the Method signature and the method name of the method. Moreover, if we do&lt;/P&gt;
&lt;P&gt;0:000&amp;gt; !dumpil 02e21430&lt;BR&gt;This is dynamic IL. Exception info is not reported at this time.&lt;BR&gt;If a token is unresolved, run "!do &amp;lt;addr&amp;gt;" on the addr given&lt;BR&gt;in parenthesis. You can also look at the token table yourself, by&lt;BR&gt;running "!DumpArray 00bc4644".&lt;/P&gt;
&lt;P&gt;IL_0000: ldstr 70000002 "Insdie DM"&lt;BR&gt;IL_0005: call 6000003 System.Console.WriteLine(System.String)&lt;BR&gt;IL_000a: ldc.i8 0&lt;/P&gt;
&lt;P&gt;We can see the IL of the method body. Well, this method has some problem so the IL body is not complete.&lt;/P&gt;
&lt;P&gt;If we do dumparray, you can find the scope table!&lt;/P&gt;
&lt;P&gt;0:000&amp;gt; !DumpArray 00bc4644&lt;BR&gt;Name: System.Object[] &lt;BR&gt;MethodTable: 5bcb9434&lt;BR&gt;EEClass: 5bcb9a14&lt;BR&gt;Size: 32(0x20) bytes&lt;BR&gt;Array: Rank 1, Number of elements 4, Type CLASS&lt;BR&gt;Element Methodtable: 5bca30dc&lt;BR&gt;[0] null&lt;BR&gt;[1] 00bc42b8&lt;BR&gt;[2] 00bc3eb0&lt;BR&gt;[3] 00bc72b8&lt;/P&gt;
&lt;P&gt;LCG has its own token table to handles and strings so this table is very important. From the IL body, we know that the string should live in the [2] position of the token table. We can verify it by doing:&lt;/P&gt;
&lt;P&gt;0:000&amp;gt; !do 00bc3eb0&lt;BR&gt;Name: System.String&lt;BR&gt;MethodTable: 5bca3760&lt;BR&gt;EEClass: 5bca36f0&lt;BR&gt;Size: 36(0x24) bytes&lt;BR&gt;&amp;nbsp;(C:\WINDOWS\Microsoft.NET\Framework\v2.0.50523dbg\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)&lt;BR&gt;String: Insdie DM&lt;BR&gt;Fields:&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MT&amp;nbsp;&amp;nbsp;&amp;nbsp; Field&amp;nbsp;&amp;nbsp; Offset&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Type VT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Attr&amp;nbsp;&amp;nbsp;&amp;nbsp; Value Name&lt;BR&gt;5bca76f4&amp;nbsp; 4000095&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Int32&amp;nbsp; 0 instance&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10 m_arrayLength&lt;BR&gt;5bca76f4&amp;nbsp; 4000096&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Int32&amp;nbsp; 0 instance&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9 m_stringLength&lt;BR&gt;5bca511c&amp;nbsp; 4000097&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Char&amp;nbsp; 0 instance&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 49 m_firstChar&lt;BR&gt;5bca3760&amp;nbsp; 4000098&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.String&amp;nbsp; 0&amp;nbsp;&amp;nbsp; shared&amp;nbsp;&amp;nbsp; static Empty&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;gt;&amp;gt; Domain:Value&amp;nbsp; 0015d8f8:5bc567a4 &amp;lt;&amp;lt;&lt;BR&gt;5bcb98d0&amp;nbsp; 4000099&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 14&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Char[]&amp;nbsp; 0&amp;nbsp;&amp;nbsp; shared&amp;nbsp;&amp;nbsp; static WhitespaceChars&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;gt;&amp;gt; Domain:Value&amp;nbsp; 0015d8f8:00bc12c8 &amp;lt;&amp;lt;&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;The Console.WriteLine's method handle should be in [3] so:&lt;/P&gt;
&lt;P&gt;0:000&amp;gt; !do 00bc72b8&lt;BR&gt;Name: System.RuntimeMethodHandle&lt;BR&gt;MethodTable: 5bcadfcc&lt;BR&gt;EEClass: 5bd7a614&lt;BR&gt;Size: 12(0xc) bytes&lt;BR&gt;&amp;nbsp;(C:\WINDOWS\Microsoft.NET\Framework\v2.0.50523dbg\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)&lt;BR&gt;Fields:&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MT&amp;nbsp;&amp;nbsp;&amp;nbsp; Field&amp;nbsp;&amp;nbsp; Offset&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Type VT&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Attr&amp;nbsp;&amp;nbsp;&amp;nbsp; Value Name&lt;BR&gt;5bca6cbc&amp;nbsp; 40004df&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.IntPtr&amp;nbsp; 0 instance &lt;FONT color=#ff0000&gt;&lt;STRONG&gt;1541425360&lt;/STRONG&gt;&lt;/FONT&gt; m_ptr&lt;BR&gt;0:000&amp;gt; !dumpmd 0n&lt;FONT color=#000000&gt;&lt;STRONG&gt;1541425360&lt;/STRONG&gt;&amp;nbsp;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Method Name: System.Console.WriteLine(System.String)&lt;BR&gt;Class: 5bd75708&lt;BR&gt;MethodTable: 5bca66f0&lt;BR&gt;mdToken: 06000767&lt;BR&gt;Module: 5bc44000&lt;BR&gt;IsJitted: yes&lt;BR&gt;m_CodeOrIL: 5b704610&lt;/P&gt;
&lt;P&gt;The two most common errors that caused InvalidProgramException are:&lt;/P&gt;
&lt;P&gt;1. You are emitting bad IL; 2. If you are using &lt;EM&gt;open scope API&lt;/EM&gt;, you could be messing up token tables. &lt;/P&gt;
&lt;P&gt;So these Sos commands should help you to identify most of the InvalidProgramExceptions.&lt;/P&gt;
&lt;P&gt;I will explain these open scope APIs, token scope in my later posts.&lt;/P&gt;
&lt;P&gt;Another easy way to check you IL stream is to emit the same IL stream using Reflection.Emit so that you can bake the method and use tools such as peverify to identify where you did wrong. &lt;/P&gt;
&lt;P&gt;Finally, can you tell me where I did wrong in this small program?&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=422373" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Start on LCG</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/05/19/420183.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/05/19/420183.aspx</id><published>2005-05-19T16:39:00Z</published><updated>2005-05-19T16:39:00Z</updated><content type="html">&lt;P&gt;I think I will start severl topics around LCG in the furture. Our PM Joel has an excellent post about various method invocations and the paragraph about LCG is a sweet start point on Lightweight Code Gen.&lt;/P&gt;
&lt;P&gt;&lt;a href="http://blogs.msdn.com/joelpob/archive/2004/04/01/105862.aspx"&gt;http://blogs.msdn.com/joelpob/archive/2004/04/01/105862.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Here is a list of basic dynamic features:&lt;/P&gt;
&lt;P&gt;1.&amp;nbsp;LCG Methods are reclaimable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;If you used Reflection.Emit, you will probably find that you cannot&amp;nbsp;delete your emitted stuff within the same appdomain. It is because Reflection.Emit stuff lives on loader heap and those memories are not reclaimable until AppDomain shutdown. For dynamic method, it's code heap can be fully reclaimed through GC reclaim, and its method desc can be resued. So you can imagine that if you are emitting a lot of methods within an Appdomain using Reflection.Emit, even though you finished using most of them, the memory usage will increase linearly until you unload the appdomain. But for dyanmic method, if you are only holding up to a certain number of methods and creating and release a lot of methods, the memory usage will be flat.&lt;/P&gt;
&lt;P&gt;2. LCG Method is a global static method on module.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;LCG Method&amp;nbsp;is bascially a piece of code that has minimium amount of metadata associated with it. Although we have a Creation API that allows you to specify a parent type for the dynamic method, the type there is only used for visibility checks -- that is the dynamic method will be able to access all private members in the type and all internal members in the type's module. Metadata-wise, it doesn't belong to that type.&lt;/P&gt;
&lt;P&gt;You can imagine then&amp;nbsp;LCG Method's can be used to write compiled regular expression evaluator, XSLT transformation sheet, database queries. It provides an efficient way for server-side code generation.&amp;nbsp;LCG methods can be invoked early bind (through Emit(OpCodes.Call, DynamicMethod)), late bind (DynamicMethod.Invoke) and through delegate.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=420183" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry><entry><title>Known Reflection.Emit Restrictions</title><link rel="alternate" type="text/html" href="http://blogs.msdn.com/yirutang/archive/2005/05/19/420134.aspx" /><id>http://blogs.msdn.com/yirutang/archive/2005/05/19/420134.aspx</id><published>2005-05-19T13:59:00Z</published><updated>2005-05-19T13:59:00Z</updated><content type="html">&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;Shreeman suggested us to publish a list of&amp;nbsp;Reflection.Emit known restrictions (that is not going to be made into Whidbey). I happen to have such a list on my machine for my own reference. So I just post them out here:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;U&gt;Cannot Emit nested enum type&lt;/U&gt; 
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l2 level1 lfo1"&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l2 level1 lfo1"&gt;Basically, we need a CreateEnum API on TypeBuilde&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l2 level1 lfo1"&gt;&lt;U&gt;Cannot Emit global field&lt;/U&gt;&amp;nbsp;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt; 
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l2 level1 lfo1"&gt;&lt;U&gt;Cannot Emit private enum field&lt;/U&gt;&amp;nbsp; 
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l2 level1 lfo1"&gt;&lt;U&gt;Small things like:&lt;/U&gt;&amp;nbsp; 
&lt;UL&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l2 level1 lfo1"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 'Courier New'"&gt;&lt;SPAN style="mso-list: Ignore"&gt;&lt;SPAN style="FONT: 7pt 'Times New Roman'"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;Cannot Emit some of the AssemblyNameFlags such as 0; Cannot Emit CallingConventions.WinAPI&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l2 level1 lfo1"&gt;&lt;U&gt;EventBuilder should derive from EventInfo&lt;/U&gt;&amp;nbsp; 
&lt;UL&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l2 level1 lfo1"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: 'Courier New'"&gt;This will probably never be fixed because if we fix this it is a breaking change from previous versions.&lt;/LI&gt;&lt;/UL&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; tab-stops: list .5in; mso-list: l1 level1 lfo2; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;U&gt;Cannot Emit new format security attribute&lt;/U&gt;&amp;nbsp; &lt;/LI&gt;&lt;/UL&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; tab-stops: list .5in"&gt;In Whidbey, Security Attributes&amp;nbsp;got a new form which has a less length in blob and the layout is closer to normal custom attributes. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; tab-stops: list .5in"&gt;For example, the MemberAccessPermission attribute looks like this in ildasm:&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-FAMILY: Fixedsys; mso-bidi-font-family: Fixedsys"&gt;.permissionset reqmin&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-FAMILY: Fixedsys; mso-bidi-font-family: Fixedsys"&gt;= {[mscorlib]System.Security.Permissions.ReflectionPermissionAttribute = {property enum class 'System.Security.Permissions.ReflectionPermissionFlag, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 'Flags' = int32(2)}}&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;The old format in &lt;?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /&gt;&lt;st1:City w:st="on"&gt;&lt;st1:place w:st="on"&gt;Everett&lt;/st1:place&gt;&lt;/st1:City&gt; is like this:&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-FAMILY: Fixedsys; mso-bidi-font-family: Fixedsys"&gt;.permissionset reqmin&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-FAMILY: Fixedsys; mso-bidi-font-family: Fixedsys"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;"&amp;lt;PermissionSet class=\"System.Security.PermissionSe"&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-FAMILY: Fixedsys; mso-bidi-font-family: Fixedsys"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;+ "t\"\r\nversion=\"1\"&amp;gt;\r\n&amp;lt;IPermission class=\"System.Security.Permis"&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-FAMILY: Fixedsys; mso-bidi-font-family: Fixedsys"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;+ "sions.ReflectionPermission, mscorlib, Version=2.0.0.0, Cultu"&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-FAMILY: Fixedsys; mso-bidi-font-family: Fixedsys"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;+ "re=neutral, PublicKeyToken=b77a5c561934e089\"\r\nversion=\"1\"\r\nF"&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;&lt;SPAN style="FONT-FAMILY: Fixedsys; mso-bidi-font-family: Fixedsys"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;+ "lags=\"MemberAccess\"/&amp;gt;\r\n&amp;lt;/PermissionSet&amp;gt;\r\n"&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;If you use Ctrl+M to open up the metdata info, you will find the new attribute's blob length is shorter.They serve the same purpose and has same&amp;nbsp;effect. The only problem is that Reflection doesn't support Reflect on the old format security attribute.&lt;/P&gt;
&lt;UL type=disc&gt;
&lt;LI class=MsoNormal style="MARGIN: 0in 0in 0pt; mso-layout-grid-align: none; tab-stops: list .5in; mso-list: l0 level1 lfo3; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;U&gt;On Method Emit, some ordering of&amp;nbsp;the API usage&amp;nbsp;can cause the method not being emitted fully right.&lt;/U&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;DIR=LTR style="MARGIN-RIGHT: 0px"&gt;
&lt;P class=MsoNormal dir=ltr style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;The order of setting custom attribute, set parameters and set implementation flags on method could matter.&lt;/P&gt;
&lt;P class=MsoNormal dir=ltr style="MARGIN: 0in 0in 0pt 0.5in; mso-layout-grid-align: none"&gt;A best practice is that you always SetImplementationFlags last and you always set return type before setting parameter types. &lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;Finally, I was asked by a user about CodeDom and Reflection.Emit comparison. I know little about CodeDom, so I'd rather not comment it here. Here is an article I found on the web that&amp;nbsp;give some light on this topic:&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;A href="http://www.fawcette.com/reports/vslivesf/2004/holmes/"&gt;http://www.fawcette.com/reports/vslivesf/2004/holmes/&lt;/A&gt;&lt;/o:p&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;o:p&gt;&lt;/o:p&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;Edited:7/2/2005&lt;/P&gt;
&lt;P&gt;Remove some restrictions since they are going to be fixed in Whidbey.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;/SPAN&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=420134" width="1" height="1"&gt;</content><author><name>yirutang</name><uri>http://blogs.msdn.com/members/yirutang.aspx</uri></author></entry></feed>