Microsoft KB Archive/327131

= BUG: RegisterAssembly cannot register a dynamic assembly =

Article ID: 327131

Article Last Modified on 5/18/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * 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.



Steps to Reproduce the Behavior
 Create a Visual C# .NET Console Application project.  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 = &quot;MyDynamicAssembly&quot;;

AssemblyBuilder myAssemblyBuilder = System.Threading.Thread.GetDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);

ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule(&quot;myModule&quot;, myAssemblyName.Name + &quot;.dll&quot; ); TypeBuilder myTypeBuilder = myModuleBuilder.DefineType( &quot;MyClass&quot;, TypeAttributes.Public);

FieldBuilder myFieldBuilder=myTypeBuilder.DefineField( &quot;intField&quot;, typeof(int),FieldAttributes.Public );

myTypeBuilder.CreateType;

Console.WriteLine( myAssemblyName.Name + &quot;.dll&quot; ); myAssemblyBuilder.Save( myAssemblyName.Name + &quot;.dll&quot; ); //Assembly myAssembly=Assembly.LoadFrom(myAssemblyName.Name+&quot;.dll&quot;); RegistrationServices myRegistrationSvr = new RegistrationServices; //bool ret=myRegistrationSvr.RegisterAssembly(myAssembly,AssemblyRegistrationFlags.None); bool ret=myRegistrationSvr.RegisterAssembly(myAssemblyBuilder,AssemblyRegistrationFlags.None);     } }                Compile and then run the application.

