BUG: GetTypeFromCLSID and GetType return incorrect type (327420)



The information in this article 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 "Applies to" section.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. 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("1.0.0.0")]
    [assembly: AssemblyKeyFile("vtest.snk")]
    
    
    [Guid("BAF27F93-D60D-4092-815E-F5382C9FBBB4")]
    public class MyClass
    {
    	public string HelloWorld() { return "Hello, World: From V1"; }
    }
    					
  2. 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("2.0.0.0")]
    [assembly: AssemblyKeyFile("vtest.snk")]
    
    
    [Guid("BAF27F93-D60D-4092-815E-F5382C9FBBB4")]
    public class MyClass
    {
    	public string HelloWorld() { return "Hello, World: From V2"; }
    }
    					
  3. Create Vtest.snk with the Sn.exe tool:
    // Test client
    using System;
    
    class Test
    {
    	static void Main(string[] args)
    	{
    		Console.WriteLine("Main");
    		Type t1 = Type.GetType("MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=fadf9433ffb9570a");
    		Console.WriteLine("GetType()          : " + t1.AssemblyQualifiedName);
    		
    		Type t2 = Type.GetTypeFromCLSID(new Guid("BAF27F93-D60D-4092-815E-F5382C9FBBB4"));
    		Console.WriteLine("GetTypeFromCLSID() : " + t2.AssemblyQualifiedName);
    
    	}	
    }
    					
  4. Save a copy of Vtest.snk in both the v1 and v2 directories:
    sn.exe -k vtest.snk
    					
  5. In their respective directories, compile both MyAssembly.cs files into DLLs.
  6. Use gacutil -i to add both versions of MyAssembly.dll to the GAC.
  7. Run Regasm.exe on version 2 of MyAssembly.dll.
  8. Save Test.cs in any directory you want.
  9. Run the following at a command line to find the PublicKeyToken for MyAssembly.dll:
    gacutil.exe -l MyAssembly
    					
  10. Edit the following line in Test.cs to include the correct PublicKeyToken:
    Type t1 = Type.GetType("MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=<insert your PublicKeyToken>");
    					
  11. Save the changes.
  12. Compile Test.cs into an .exe file.
  13. Run Test.exe. The output that is generated will look similar to the following:
    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.

Modification Type:MinorLast Reviewed:8/24/2005
Keywords:kbvs2002sp1sweep kbbug KB327420