BUG: RegisterAssembly cannot register a dynamic assembly (327131)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1

This article was previously published under Q327131

SYMPTOMS

When you register a dynamically generated assembly by using the System.Runtime.InteropServices.RegistrationServices.RegisterAssembly method, you may receive the following error message:
An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll

Additional information: The invoked member is not supported in a dynamic assembly.

CAUSE

The problem occurs because RegisterAssembly calls GetRegistrableTypesInAssembly, which in turn calls GetExportedTypes on the Assembly instance. However, this type is an AssemblyBuilder, which derives from Assembly and then overrides GetExportedTypes to throw a NotSupportedException.

RESOLUTION

To work around this problem, save the dynamic assembly to a file and then register it from the disk.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION


Steps to Reproduce the Behavior

  1. Create a Visual C# .NET Console Application project.
  2. Replace the auto-generated code in the Class1.cs file with the following code:
    using System;
    using System.Reflection;
    using System.Reflection.Emit;
    using System.Configuration.Assemblies;
    using System.Runtime.InteropServices;
    
    class EnumBuilderWorkaround
    {
    	public static void Main()
    	{
    		AssemblyName myAssemblyName = new AssemblyName();
    		myAssemblyName.Name = "MyDynamicAssembly";
    
    		AssemblyBuilder myAssemblyBuilder = System.Threading.Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave); 
    
    		ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("myModule", myAssemblyName.Name + ".dll" );
       	
    		TypeBuilder myTypeBuilder = myModuleBuilder.DefineType( "MyClass", TypeAttributes.Public);
    
    		FieldBuilder myFieldBuilder=myTypeBuilder.DefineField( "intField", typeof(int),FieldAttributes.Public );
    
    		myTypeBuilder.CreateType();
    
    		Console.WriteLine( myAssemblyName.Name + ".dll" );
    		myAssemblyBuilder.Save( myAssemblyName.Name + ".dll" );   	
    	
    		//Assembly myAssembly=Assembly.LoadFrom(myAssemblyName.Name+".dll");  
    		RegistrationServices myRegistrationSvr = new RegistrationServices();
    		//bool ret=myRegistrationSvr.RegisterAssembly(myAssembly,AssemblyRegistrationFlags.None); 
    		bool ret=myRegistrationSvr.RegisterAssembly(myAssemblyBuilder,AssemblyRegistrationFlags.None);		}
    }
    				
  3. Compile and then run the application.

REFERENCES

For additional information, visit the following Microsoft Web sites:

Exposing .NET Framework Components to COM
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconexposingnetframeworkcomponentstocom.asp

MSIL Disassembler (Ildasm.exe)
http://msdn.microsoft.com/library/en-us/cptools/html/cpconmsildisassemblerildasmexe.asp

Modification Type:MinorLast Reviewed:9/14/2005
Keywords:kbvs2002sp1sweep kbbug kbCOMInterop kbpending KB327131 kbAudDeveloper