Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / December 2003

Tip: Looking for answers? Try searching our database.

Reflection.Emit problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Zanna - 25 Nov 2003 20:50 GMT
Hi all,

I hope this is the right NG for this question.

I got a problem generating IL via Reflection.Emit.

I cannot generate the code for this simple class:

class Class1

{

   public void Test()

   {

       object x;

       x = new Class1();

   }

}

The IL should be this:

   .class private auto ansi beforefieldinit Class1
      extends [mscorlib]System.Object
   {

       .method public hidebysig specialname rtspecialname instance void
.ctor() cil managed
       {
           .maxstack 1

     IL_0000:  ldarg.0
     IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
     IL_0006:  ret
       }

       .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
       }
   }
My problem is the

   System.Type myType = CodeBuilder.GetType(myModuleBuilder, className)
   myILGenerator.Emit(OpCodes.Newobj,
myType.GetConstructor(parameterTypes));

Because the myType.GetConstructor(parameterTypes) (parameterTypes is
Type.EmptyTypes) because it throw the "System.NotSupportedException" with
the description "The called member is not supported within the dynamic
module".

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?

Thank you

--
Math Parser : http://www.neodatatype.net
Alicia Li - 03 Dec 2003 18:14 GMT
Below is the sample code to do it:

using System;
using System.Reflection;
using System.Reflection.Emit;

public class TestClass
{
    public static void Main()
    {
       
        AssemblyName asmname = new AssemblyName();
        asmname.Name = "tempfile_Asm";

        AssemblyBuilder asmbuild = System.Threading.Thread.GetDomain().
            DefineDynamicAssembly(asmname, AssemblyBuilderAccess.RunAndSave);   //
run and save only assembly
        ModuleBuilder mb = asmbuild.DefineDynamicModule( "TestModule",
asmname.Name + ".dll" );
           TypeBuilder tb = mb.DefineType( "MyType", TypeAttributes.Public );

        ConstructorBuilder corb = tb.DefineDefaultConstructor(
MethodAttributes.Public );
       
        MethodBuilder methb = tb.DefineMethod( "Test", MethodAttributes.Public,
null, null );
        ILGenerator lgen = methb.GetILGenerator();
        lgen.DeclareLocal( typeof( Object ) );
        lgen.Emit( OpCodes.Newobj, corb );
        lgen.Emit( OpCodes.Stloc_0 );
        lgen.Emit( OpCodes.Ret );

        tb.CreateType();

        Console.WriteLine( "Saving assembly file " + asmname.Name + ".dll" );
        asmbuild.Save( asmname.Name + ".dll" );

        Assembly asm = Assembly.LoadFrom( asmname.Name + ".dll" );
        Type t = asm.GetType( "MyType" );
        Console.WriteLine( "t=" + t );

    }
}

Thanks
Alicia[MSFT]

--------------------
>From: "Zanna" <znt.fabio@virgilio.it>
>Newsgroups: microsoft.public.dotnet.framework.clr
[quoted text clipped - 9 lines]
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
>Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.su
l.t-online.de!newsfeed01.sul.t-online.de!t-online.de!fu-berlin.de!uni-berlin
.de!host188-20.pool21345.interbusiness.IT!not-for-mail
>Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.framework.clr:8790
>X-Tomcat-NG: microsoft.public.dotnet.framework.clr
[quoted text clipped - 67 lines]
>--
>Math Parser : http://www.neodatatype.net

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.