reflection.emit

Create DynamicMethod to assign value to a property?

巧了我就是萌 提交于 2019-11-30 15:37:22
问题 This is a learning exercise. I created a method that takes a Foo and a string and sets the A property. I used the Reflector disassembly to make the following emit code. The disassembly looks like this: .method private hidebysig static void Spork(class ConsoleTesting.Foo f, string 'value') cil managed { .maxstack 8 L_0000: ldarg.0 L_0001: ldarg.1 L_0002: callvirt instance void ConsoleTesting.Foo::set_A(string) L_0007: ret } Ok, so I modeled my emit code after that: using System; using System

Dynamically create type and call constructor of base-class

风流意气都作罢 提交于 2019-11-30 12:06:48
I need to create a class dynamically. Most things work fine but i'm stuck in generating the constructor. AssemblyBuilder _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyBuilder"), AssemblyBuilderAccess.Run); ModuleBuilder _moduleBuilder = _assemblyBuilder.DefineDynamicModule("MyModule"); public static object GetInstance<TSource, TEventArgs>(this TSource source, string eventName) where TSource : class { var typeName = "MyTypeName"; var typeBuilder = _moduleBuilder.DefineType(typeName, TypeAttributes.Class | TypeAttributes.Public); // create type like class

Creating method dynamically, and executing it

北城余情 提交于 2019-11-30 10:41:19
问题 Background: I want to define few static methods in C# , and generate IL code as byte array, from one of these methods, selected at runtime (on client), and send the byte array over network to another machine (server) where it should be executed after re-generating the IL code from the byte array. My Attempt: (POC) public static class Experiment { public static int Multiply(int a, int b) { Console.WriteLine("Arguments ({0}, {1})", a, b); return a * b; } } And then I get the IL code of the

Howto emit a delegate or lambda expression

删除回忆录丶 提交于 2019-11-29 23:59:17
问题 I want to emit a method that returns a Func<>. Inside this method I have to create a delegate or a lambda expression which exactly serves the return type. Altogether it should look like this: // I have a resolve method that will be called inside my missing method // This is it's signature: object Resolve( params object[] args); // This is how I use it: var barFactory = ( Func<IBar> )MissingMethod( typeof( IBar ) ); var bar = barFactory.Invoke(); // or - with one string argument: var

Creating method dynamically, and executing it

懵懂的女人 提交于 2019-11-29 21:25:14
Background: I want to define few static methods in C# , and generate IL code as byte array, from one of these methods, selected at runtime (on client), and send the byte array over network to another machine (server) where it should be executed after re-generating the IL code from the byte array. My Attempt: ( POC ) public static class Experiment { public static int Multiply(int a, int b) { Console.WriteLine("Arguments ({0}, {1})", a, b); return a * b; } } And then I get the IL code of the method body, as: BindingFlags flags = BindingFlags.Public | BindingFlags.Static; MethodInfo meth = typeof

Dynamically create type and call constructor of base-class

隐身守侯 提交于 2019-11-29 18:00:05
问题 I need to create a class dynamically. Most things work fine but i'm stuck in generating the constructor. AssemblyBuilder _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyBuilder"), AssemblyBuilderAccess.Run); ModuleBuilder _moduleBuilder = _assemblyBuilder.DefineDynamicModule("MyModule"); public static object GetInstance<TSource, TEventArgs>(this TSource source, string eventName) where TSource : class { var typeName = "MyTypeName"; var typeBuilder =

Is there kind of runtime C++ assembler library around? [closed]

六月ゝ 毕业季﹏ 提交于 2019-11-29 04:23:50
For my small hobby project I need to emit machine code from C++ program in runtime. I have base address 0xDEADBEEF and want to write something like this: Assembler a((void*)0xDEADBEEF); a.Emit() << Push(Reg::Eax) << Push(Reg::Ebx) << Jmp(0xFEFEFEFE); Inline assembler isn't my choice because generated machine code is dependent of the program state. Does anybody know any existing library for doing this? If no, would it be a good idea to develop one from scratch and make it open source? (I mean, will anybody ever use this library if it existed?) You could use Nicolas Capen's softwire . Its really

Reflect.Emit Dynamic Type Memory Blowup

Deadly 提交于 2019-11-29 02:49:10
问题 Using C# 3.5 I am trying to generate dynamic types at runtime using reflection emit. I used the Dynamic Query Library sample from Microsoft to create a class generator. Everything works, my problem is that 100 generated types inflate the memory usage by approximately 25MB. This is a completely unacceptable memory profile as eventually I want to support having several hundred thousand types generated in memory. Memory profiling shows that the memory is apparently being held by various System

Cannot bind to the target method when creating delegates for properties

房东的猫 提交于 2019-11-29 02:09:47
Trying to create two dictionaries of emitted delegates to allow for improved performance when dynamically getting/setting the values of properties. Code: Properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance) .Where(p => p.CanRead && !p.GetIndexParameters().Any()) .AsEnumerable(); PropertyGetters = Properties.ToDictionary(p => p.Name, p => (Func<object, object>)Delegate.CreateDelegate(typeof(Func<object, object>), p.GetGetMethod())); PropertySetters = Properties.Where(p => p.GetSetMethod() != null) .ToDictionary(p => p.Name, p => (Action<object, object>)Delegate

Java Equivalent of Reflection.Emit

守給你的承諾、 提交于 2019-11-29 01:17:47
As far as I can tell, Java has no such equivalent of C#'s Reflection.Emit stuff. Are there any additional libraries for Java that provide similar functionality? What are the differences (to reflection emit )? Besides Darin's excellent answer (+1), ASM is worth checking out too. The Byte Code Engineering Library (BCEL) 来源: https://stackoverflow.com/questions/2259323/java-equivalent-of-reflection-emit