Can you save a type created with TypeBuilder?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm having difficulty with creating dynamic types, TypeBuilder is attained through module builder which in turn comes from assembly builder. I assumed that if you save the assembly you'd save the type (made with the typebuilder) as well. When I save and load the assembly it comes back as having no types
Is there a certain set of rules the type or assembly must comply to for it to be valid
If so, what might those rules be

I greatly appreciate any assistance on the matter

Jax
 
Jax said:
I'm having difficulty with creating dynamic types, TypeBuilder is
attained through module builder which in turn comes from assembly
builder. I assumed that if you save the assembly you'd save the type
(made with the typebuilder) as well. When I save and load the assembly
it comes back as having no types.
Is there a certain set of rules the type or assembly must comply to
for it to be valid?
If so, what might those rules be?

It should be fine, so long as you've called TypeBuilder.CreateType.
Could you post a short but complete example of the problem?
 
Thanks for replying Jon

Creating a type from a form
The form returns to this method an arraylist of TypeObjects the object contains 2 strings, name and type. example: "Width" "System.Int32
I try to match the types at the end and my messageboxes tell me the results
The first messageBox matches fine
Then I save and load the assembly and attempt a further match, this one is always false, in debugger the loaded assembly has no types

AssemblyName an = new AssemblyName()
an.Name = "myAssembly"
AppDomain currentDomain = AppDomain.CurrentDomain
AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(an, AssemblyBuilderAccess.RunAndSave)
ModuleBuilder mb = ab.DefineDynamicModule("myModule")
TypeBuilder tBuild = mb.DefineType("myType", TypeAttributes.Public)
foreach(TypeObject to in alTypeObjects

tBuild.DefineField(to.name, System.Type.GetType(to.type),FieldAttributes.Private)
System.Type[] types = new System.Type[1]
types[0] = System.Type.GetType(to.type)
tBuild.DefineProperty(to.type.ToUpper(), PropertyAttributes.HasDefault, System.Type.GetType(to.type), types);


Type theType = tBuild.CreateType()
object o = Activator.CreateInstance(theType)

Type aType = mb.GetType("myType")
if(aType == theType
MessageBox.Show("Type match")
ab.Save("myassembly.dll")
Assembly a = Assembly.LoadFrom("myassembly.dll")
aType = a.GetType("myType")
if(aType == theType
MessageBox.Show("Type match")
els
{MessageBox.Show("2nd match not found");}
 
Back
Top