Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 327131 - Last Review: May 18, 2007 - Revision: 3.8
BUG: RegisterAssembly cannot register a dynamic assembly
This article was previously published under Q327131
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.
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.
To work around this problem, save the dynamic assembly to a
file and then register it from the disk.
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 = "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); }
}
- Compile and then run the application.
For additional information, visit the following Microsoft
Web sites:
Exposing .NET Framework Components to COM
http://msdn2.microsoft.com/en-us/library/zsfww439(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/zsfww439(vs.71).aspx)
MSIL
Disassembler (Ildasm.exe)
http://msdn2.microsoft.com/en-us/library/f7dy01k1(vs.71).aspx
(http://msdn2.microsoft.com/en-us/library/f7dy01k1(vs.71).aspx)
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
| kbvs2002sp1sweep kbbug kbcominterop kbpending KB327131 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate