Results 1 to 1 of 1
  1. #1
    Member Pentagasm's Avatar
    Reputation
    12
    Join Date
    Sep 2017
    Posts
    4
    Thanks G/R
    1/2
    CoreCoins
    15
    Trade Feedback
    0 (0%)

    Class for Dynamic Method Creation

    Hey all. I was working on my hacking library and I came across the need to create some methods using IL. Here is a class I wrote, hopefully someone finds it useful.

    The Class:
    Code:
        public static class DynamicMethodFactory
        {
            static AssemblyName assemblyName;
            static AssemblyBuilder assembly;
            static ModuleBuilder module;
    
            static DynamicMethodFactory()
            {
                assemblyName = new AssemblyName("DynamicMethodFactory");
                assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
                module = assembly.DefineDynamicModule($"<Module-{Guid.NewGuid()}>");
            }
    
            public static T CreateDelegate<T>(Action<ILGenerator> generate) where T : class
            {
                if (!typeof(T).IsSubclassOf(typeof(Delegate)))
                    throw new ArgumentException("T must be a delegate type.");
    
                var type = module.DefineType($"<Type-{Guid.NewGuid()}>");
                var methodName = $"<Method-{Guid.NewGuid()}>";
                var methodInfo = typeof(T).GetMethod("Invoke");
                var method = type.DefineMethod(methodName, MethodAttributes.Static | MethodAttributes.Public, methodInfo.ReturnType, methodInfo.GetParameters().Select(x => x.ParameterType).ToArray());
                
                generate(method.GetILGenerator());
                type.CreateType();
    
                return type.GetMethod(methodName).CreateDelegate(typeof(T)) as T;
            }
        }
    Possible Use:
    Code:
        public static class StructHelper<T> where T : struct
        {
            private delegate IntPtr AddressOfDelegate(ref T value);
    
            public static AddressOfDelegate AddressOf { get; }
    
            static StructHelper()
            {
                AddressOf = DynamicMethodFactory.CreateDelegate<AddressOfDelegate>(il =>
                {
                    il.Emit(OpCodes.Ldarg, 0);
                    il.Emit(OpCodes.Ret);
                });
            }
        }

 

 

Similar Threads

  1. Custom classes for Glider Elite
    By azzi88 in forum World of Warcraft Bots and Programs
    Replies: 13
    Last Post: 06-04-2008, 03:12 PM
  2. reskins for all classes for both male and female
    By Corroasive in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 05-17-2008, 11:08 AM
  3. Looking for dedicated acount creation page guide
    By Bloodstain in forum World of Warcraft Emulator Servers
    Replies: 2
    Last Post: 03-23-2008, 03:29 PM
  4. [Request] Shaman Custom class for Mmoglider
    By lohkies in forum World of Warcraft General
    Replies: 5
    Last Post: 02-05-2008, 12:31 AM
  5. [Help] Website for TBC acc creation
    By Gadella in forum World of Warcraft Emulator Servers
    Replies: 6
    Last Post: 12-19-2007, 07:08 AM
All times are GMT -5. The time now is 01:17 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2017 vBulletin Solutions, Inc. All rights reserved. Digital Point modules: Sphinx-based search