Results 1 to 4 of 4
  1. #1
    t7ancients's Avatar
    Join Date
    Dec 2008
    Gender
    male
    Location
    New York
    Posts
    381
    Reputation
    28
    Thanks
    68
    My Mood
    Twisted

    System.Reflection.Emit?

    Does anyone know anything about this namespace, or about dynamic methods, or dynamic code generation? If so, please give me a basic rundown lol.

  2. #2
    freedompeace's Avatar
    Join Date
    Jul 2010
    Gender
    female
    Posts
    3,033
    Reputation
    340
    Thanks
    2,792
    My Mood
    Sad
    hello, world... Reflection.Emit style! - Joel Pobar's CLR weblog - Site Home - MSDN Blogs

    I've used it before to dynamically detour functions (getting rid of that XNA 4 Hi Profile checking shit), but I'm not sure of any other reasons I'd use it...

  3. The Following User Says Thank You to freedompeace For This Useful Post:

    t7ancients (10-04-2011)

  4. #3
    Hassan's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    System.Threading.Tasks
    Posts
    4,764
    Reputation
    495
    Thanks
    2,133
    My Mood
    Dead
    The primary functionality of this namespace is to provide runtime code generation services. Starting with .NET 2.0, services to emit code at runtime have been added to the framework. This is known as Lightweight code generation. The use of this API requires a thorough understanding of IL. But here's a simple example of how you can generate a run-time code:

    [highlight=c#]using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.Reflection.Emit;

    namespace LCGConsole
    {
    class Program
    {
    static void Main(string[] args)
    {
    //Create a runtime assembly that will not be saved to disk.
    var Asm = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyAssembly"), AssemblyBuilderAccess.Run);
    //Create a dynamic module.
    var Module = Asm.DefineDynamicModule("MyModule");
    //Create a type that will enclose our method.
    var Type = Module.DefineType("MyClass");
    //Create a static method which will perform some operation. Make it static and public so it can be accessed by the Main.
    var Method = Type.DefineMethod("SimpleHelloWorld",MethodAttribu tes.Static | MethodAttributes.Public);
    //Get MSIL Generator for our method.
    var GenerateILCode = Method.GetILGenerator();
    //Emit IL Code. In this case 'Ldstr' means LoadString. We load 'Hello World ;]' to the IL stack.
    GenerateILCode.Emit(OpCodes.Ldstr, "Hello World ;]");
    //Emit IL Code for calling the 'WriteLine' method using Console.
    GenerateILCode.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", new[] { typeof(string) }, null));
    //Emit IL code to return the result of last call.
    GenerateILCode.Emit(OpCodes.Ret);
    //Create the type from our Dynamic Type Definition.
    var Result = Type.CreateType();
    //Finally, invoke the method with no parameters.
    Result.GetMethod("SimpleHelloWorld").Invoke(null, null);
    Console.ReadLine();
    }
    }
    }[/highlight]

    I've commented the code. Guess shouldn't be hard for you to understand.

  5. The Following User Says Thank You to Hassan For This Useful Post:

    t7ancients (10-04-2011)

  6. #4
    t7ancients's Avatar
    Join Date
    Dec 2008
    Gender
    male
    Location
    New York
    Posts
    381
    Reputation
    28
    Thanks
    68
    My Mood
    Twisted
    Thanks guys.

Similar Threads

  1. Simple Reflective Text
    By Chronologix in forum Tutorials
    Replies: 4
    Last Post: 05-20-2006, 05:56 AM
  2. Unpacked system.mrs
    By 1337Sasuke in forum Gunz Hacks
    Replies: 1
    Last Post: 03-22-2006, 02:05 AM
  3. EndLess' edited system.mrs
    By EndLess in forum Gunz Hacks
    Replies: 22
    Last Post: 02-22-2006, 09:22 AM
  4. [Request] System.mrs
    By Xgs in forum Gunz General
    Replies: 10
    Last Post: 02-21-2006, 12:11 PM
  5. System Hacker For Gunz
    By gameking85 in forum Gunz Hacks
    Replies: 11
    Last Post: 02-09-2006, 11:57 AM