Microsoft KB Archive/327420

= BUG: GetTypeFromCLSID and GetType return incorrect type =

Article ID: 327420

Article Last Modified on 8/24/2005

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition

-



This article was previously published under Q327420



SYMPTOMS
You have two versions of an assembly named MyAssembly. MyAssembly contains a class that is named MyClass, and you load MyClass from version 1.0.0.0 of MyAssembly by calling GetType. Then, if you try to load MyClass from MyAssembly version 2.0.0.0 by calling GetTypeFromCLSID or GetType, the type that is returned from this call is the type for MyAssembly version 1.0.0.0.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
  Use the following code to create a file named MyAssembly.cs for version 1, and save it in a directory named v1: // MyAssembly.cs code for assembly version 1 using  System; using  System.Reflection; using  System.Runtime.InteropServices;

[assembly: AssemblyVersion(&quot;1.0.0.0&quot;)] [assembly: AssemblyKeyFile(&quot;vtest.snk&quot;)]

[Guid(&quot;BAF27F93-D60D-4092-815E-F5382C9FBBB4&quot;)] public class MyClass {   public string HelloWorld { return &quot;Hello, World: From V1&quot;; } }                     Use the following code to create a file named MyAssembly.cs for version 2, and save it in a directory named v2: // MyAssembly.cs code for assembly version 2 using  System; using  System.Reflection; using  System.Runtime.InteropServices;

[assembly: AssemblyVersion(&quot;2.0.0.0&quot;)] [assembly: AssemblyKeyFile(&quot;vtest.snk&quot;)]

[Guid(&quot;BAF27F93-D60D-4092-815E-F5382C9FBBB4&quot;)] public class MyClass {   public string HelloWorld { return &quot;Hello, World: From V2&quot;; } }                     Create Vtest.snk with the Sn.exe tool: // Test client using System;

class Test {   static void Main(string[] args) {       Console.WriteLine(&quot;Main&quot;); Type t1 = Type.GetType(&quot;MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fadf9433ffb9570a&quot;); Console.WriteLine(&quot;GetType         : &quot; + t1.AssemblyQualifiedName); Type t2 = Type.GetTypeFromCLSID(new Guid(&quot;BAF27F93-D60D-4092-815E-F5382C9FBBB4&quot;)); Console.WriteLine(&quot;GetTypeFromCLSID : &quot; + t2.AssemblyQualifiedName);

}  }                      Save a copy of Vtest.snk in both the v1 and v2 directories: sn.exe -k vtest.snk  In their respective directories, compile both MyAssembly.cs files into DLLs. Use gacutil -i to add both versions of MyAssembly.dll to the GAC.</li> Run Regasm.exe on version 2 of MyAssembly.dll.</li> Save Test.cs in any directory you want.</li>  Run the following at a command line to find the PublicKeyToken for MyAssembly.dll: gacutil.exe -l MyAssembly </li>  Edit the following line in Test.cs to include the correct PublicKeyToken: Type t1 = Type.GetType(&quot;MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<insert your PublicKeyToken>&quot;); </li> Save the changes.</li> Compile Test.cs into an .exe file.</li>  Run Test.exe. The output that is generated will look similar to the following: <pre class="fixed_text">GetType         : MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fadf9433ffb9570a GetTypeFromCLSID : MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fadf9433ffb9570a Notice that both lines specify version 1.0.0.0. </li></ol>

Keywords: kbbug kbvs2002sp1sweep KB327420

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.