Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

 

class MyType {
   public void MyMethod () {
       MyType x;
   }
}

 

Can I create this class via Reflection.Emit?

 

I hope I was clear :P

> NeoDataType.net <

Try my Free .Net Reporting Tool!

  • *Experts*
Posted
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.
Posted

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:

 

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

 

parameterTypes is defined as

 

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 :)

> NeoDataType.net <

Try my Free .Net Reporting Tool!

  • *Experts*
Posted
Try calling it like this:
myType.GetConstructor(BindingFlags.Instance | BindingFlags.Public, null, Type.EmptyTypes, null);

You may need to specify the BindingFlags.

Posted

Try calling it like this:

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

 

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

 

(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?

> NeoDataType.net <

Try my Free .Net Reporting Tool!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...