It appears that once you use Marshal.GetITypeInfoForType on a dynamically
generated class (using TypeBuilder), and then dynamically generate another
class in the same assembly, that trying to use Marshal.GetITypeInfoForType
for the new class will throw a COMException("Element not found.").

The following pseudocode will get an exception on the last line:

... (Both myTypeBuilder1 and myTypeBuilder2 are for the same assembly)
Type newType1 = myTypeBuilder1.CreateType();
IntPtr ptr1 = Marshal.GetITypeInfoForType(newType1);
Type newType2 = myTypeBuilder2.CreateType();
IntPtr ptr2 = Marshal.GetITypeInfoForType(newType2);  <=== Get a
COMException

However, the following pseudocode does not have the problem:

... (Both myTypeBuilder1 and myTypeBuilder2 are for the same assembly)
Type newType1 = myTypeBuilder1.CreateType();
Type newType2 = myTypeBuilder2.CreateType();
IntPtr ptr1 = Marshal.GetITypeInfoForType(newType1);
IntPtr ptr2 = Marshal.GetITypeInfoForType(newType2);

Can anyone confirm this as a bug?... or is there a valid reason for this
behavior?

While the second approach can be used on dynamically created assemblies
which are created and then remain static during execution, this is not
possible in solutions which continue to dynamically create types during the
course of execution (the first approach).  It appears that the only way to
get around this is to put each new type into a seperate assembly.  While I
expect this should work, it certainly seems very heavy handed.

Comments?

Bill Baird
File Express, Inc.

You can read messages from the Advanced DOTNET archive, unsubscribe from Advanced 
DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to