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 / October 2003

Tip: Looking for answers? Try searching our database.

About SetLocalSymInfo

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
xiaorun huang - 24 Oct 2003 00:43 GMT
when I call SetLocalSymInfo in my code within a global function, it throw
System.NullReferenceException. why?

by the way, what is the mean of startOffset and endOffset, is it offset from
file begin in charactor, or from the method's begin.

thanks

xiaorun huang
Alicia Li - 30 Oct 2003 02:54 GMT
LocalBuilder.SetLocalSymInfo() seems work fine in my below code. Can you
post a simple repro code for the problem that you ran into?
Regarding startoOffset and endOffset, they seem not affect anything. I will
follow up with this issue.

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

class TestClass
{

public static void Main()
{

        // Create an assembly.
        AssemblyName myAssemblyName = new AssemblyName();
        myAssemblyName.Name = "SampleAssembly";

        AssemblyBuilder myAssembly =
Thread.GetDomain().DefineDynamicAssembly(
           myAssemblyName, AssemblyBuilderAccess.RunAndSave);

        // Create a module 'myModule'.
        ModuleBuilder
myModule=myAssembly.DefineDynamicModule("SampleModule",
myAssemblyName.Name+".dll", true);

        // Define a public class 'myClass'.
        TypeBuilder myTypeBuilder = myModule.DefineType("myClass",
TypeAttributes.Public);

        // Define a private String field.
        FieldBuilder myField = myTypeBuilder.DefineField("myMessage",
typeof(String), FieldAttributes.Private);

        // Create the constructor.

        ConstructorBuilder myConstructor = myTypeBuilder.DefineConstructor(
           MethodAttributes.Public, CallingConventions.Standard, new
Type[]{});

        // Generate IL for the method.
        ILGenerator myConstructorIL = myConstructor.GetILGenerator();
        myConstructorIL.Emit(OpCodes.Ret);

        // Create the 'Function1' public method.
        MethodBuilder myMethod = myTypeBuilder.DefineMethod("Function1",
           MethodAttributes.Public, typeof(String), null);

        // Generate IL for 'Function1'.
        ILGenerator myMethodIL = myMethod.GetILGenerator();

        // Create local variables.
        LocalBuilder myLB1 = myMethodIL.DeclareLocal(typeof(string));
        myLB1.SetLocalSymInfo("myString1");
        Console.WriteLine("'myLB1' type is :{0}", myLB1.LocalType);
        LocalBuilder myLB2 = myMethodIL.DeclareLocal(typeof(String));
        myLB2.SetLocalSymInfo("myString2",0,0);
        Console.WriteLine("'myLB2' type is :{0}", myLB2.LocalType);
        myMethodIL.Emit(OpCodes.Ldstr, "Local value"  );
        myMethodIL.Emit(OpCodes.Stloc_0 );
        myMethodIL.Emit(OpCodes.Ldloc_0 );
        myMethodIL.Emit(OpCodes.Stloc_1);
        myMethodIL.Emit(OpCodes.Ldloc_1 );
        myMethodIL.Emit(OpCodes.Ret );

       myMethod = myModule.DefineGlobalMethod("Globalfunc",
           MethodAttributes.Public|MethodAttributes.Static,
typeof(String), null);

        // Generate IL for 'Globalfunc'.
        ILGenerator myMethodIL2 = myMethod.GetILGenerator();

        // Create local variables.
        LocalBuilder myLB3 = myMethodIL2.DeclareLocal(typeof(string));
        myLB1.SetLocalSymInfo("myString1");
        Console.WriteLine("'myLB3' type is :{0}", myLB3.LocalType);
        LocalBuilder myLB4 = myMethodIL2.DeclareLocal(typeof(String));
        myLB2.SetLocalSymInfo("myString2",0,0);
        Console.WriteLine("'myLB4' type is :{0}", myLB4.LocalType);
        myMethodIL2.Emit(OpCodes.Ldstr, "Local value"  );
        myMethodIL2.Emit(OpCodes.Stloc_0 );
        myMethodIL2.Emit(OpCodes.Ldloc_0 );
        myMethodIL2.Emit(OpCodes.Stloc_1);
        myMethodIL2.Emit(OpCodes.Ldloc_1 );
        myMethodIL2.Emit(OpCodes.Ret );

        // Create "myClass" class.
        Type myType1 = myTypeBuilder.CreateType();
    myModule.CreateGlobalFunctions();
        Console.WriteLine("'myClass' is created.");
   
    myAssembly.Save( "SampleAssembly.dll");

    MethodInfo minfo = myType1.GetMethod( "Function1" );
    Object obj = Activator.CreateInstance(myType1);
    String sret = (String)minfo.Invoke( obj, new Object[] {} );
    Console.WriteLine( "instance method return value: " + sret );

    Assembly asm = Assembly.LoadFrom( "SampleAssembly.dll");
    MethodInfo gminfo = asm.GetModules()[0].GetMethod( "Globalfunc");
    sret = (String)gminfo.Invoke( null, new Object[] {} );   
    Console.WriteLine( "global return value: " + sret );   
}
}

Rate this thread:







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.