Reflection.Emit question

neodatatype

Regular
Joined
Aug 18, 2003
Messages
65
Location
Italy
Hi all,

I'm playing with the Reflection.Emit, and all was going on when I got a little stop:

I created my AssemblyName and my TypeBuilder.
I like to do a myMethodBuilder.GetILGenerator().DeclareLocal() of the type I'm being building...

I explain :)

I get my TypeBuilder (for "MyType" class), I get the TypeBuilder.DefineMethod().
Within thid MethodBuilder (before the myTypeBuilder.CreateType()) I whant to declare a variable of "MyType" but I cannot get the System.Type.GetType("MyType")!!!

In C# it would be something like

C#:
class MyType {
    public void MyMethod () {
        MyType x;
    }
}

Can I create this class via Reflection.Emit?

I hope I was clear :P
 
Use ModuleBuilder.GetType (use the name of the ModuleBuilder you're using to define the types) to retrieve unfinished types. You can't use the regular GetType keyword for that.
 
Use ModuleBuilder.GetType (use the name of the ModuleBuilder you're using to define the types) to retrieve unfinished types.

Yes, I thinked this but I could not understand how to do it.
Using the ModuleBuilder works well, thanks :)

Now the problem is moved a step over :)

I can get the

myType = ModuleBuilder.GetType("MyType"), but I cannot do this:

C#:
myILGenerator.Emit(OpCodes.Newobj, myType.GetConstructor(parameterTypes));

parameterTypes is defined as

C#:
System.Type[] parameterTypes = System.Type.EmptyTypes;

since the constructor does not accept arguments, but the Newobj throw the System.NotSupportedException (not documented for GetConstructor), with a description like "The called member is not supported in a dynamic module" (this is translated).

Some ideas?

Thank you again :)
 
Try calling it like this:
C#:
myType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, Type.EmptyTypes, null);
You may need to specify the BindingFlags.
 
Try calling it like this:
C#:
myType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, Type.EmptyTypes, null);
You may need to specify the BindingFlags.

No way :(

I cannot do a class like

C#:
class Class1
{
    public void Test()
    {
        object x;
        x = new Class1();
    }
}

Code:
(IL)
        .method public hidebysig instance void Test() cil managed
        {
            .maxstack 1
            .locals (object)

      IL_0000:  newobj     instance void ConsoleApplication3.Class1::.ctor()
      IL_0005:  stloc.0
      IL_0006:  ret
        }

the myType.GetConstructor(parameterTypes) still throw the exception...

It Seems that I cannot call the GetConstructor with a type defined in a
dynamic module... but if so how can I generate code like the above?
 
Back
Top