ilgenerator

generics with IL?

限于喜欢 提交于 2019-12-03 15:48:29
Is it possible to use generics with the IL Generator? DynamicMethod method = new DynamicMethod( "GetStuff", typeof(int), new Type[] { typeof(object) }); ILGenerator il = method.GetILGenerator(); ... etc Yes, it is possible, but not with the DynamicMethod class. If you are restricted to using this class, you're out of luck. If you can instead use a MethodBuilder object, read on. Emitting the body of a generic method is, for most intents and purposes, no different from emitting the body of other methods, except that you can make local variables of the generic types. Here is an example of

Is there a good wrapper around ILGenerator? [closed]

天大地大妈咪最大 提交于 2019-12-03 07:11:08
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. I'm using System.Reflection.Emit for a while now, and find it (who don't?) as painful as bug prone. Do you know if there is a good wrapper around the IL Generator, something that I can rely on to emit IL in a more safe and easier manner than with playing

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

Dynamic object property populator (without reflection)

感情迁移 提交于 2019-11-30 07:48:06
I want to populate an object's properties without using reflection in a manner similar to the DynamicBuilder on CodeProject . The CodeProject example is tailored for populating entities using a DataReader or DataRecord. I use this in several DALs to good effect. Now I want to modify it to use a dictionary or other data agnostic object so that I can use it in non DAL code --places I currently use reflection. I know almost nothing about OpCodes and IL. I just know that it works well and is faster than reflection. I have tried to modify the CodeProject example and because of my ignorance with IL,

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 =

Dynamic object property populator (without reflection)

▼魔方 西西 提交于 2019-11-29 09:54:52
问题 I want to populate an object's properties without using reflection in a manner similar to the DynamicBuilder on CodeProject. The CodeProject example is tailored for populating entities using a DataReader or DataRecord. I use this in several DALs to good effect. Now I want to modify it to use a dictionary or other data agnostic object so that I can use it in non DAL code --places I currently use reflection. I know almost nothing about OpCodes and IL. I just know that it works well and is

How to mutate a boxed struct using IL

人盡茶涼 提交于 2019-11-28 20:05:31
问题 Imagine we have a mutable struct (yes, don't start): public struct MutableStruct { public int Foo { get; set; } public override string ToString() { return Foo.ToString(); } } Using reflection, we can take a boxed instance of this struct and mutate it inside the box: // this is basically what we want to emulate object obj = new MutableStruct { Foo = 123 }; obj.GetType().GetProperty("Foo").SetValue(obj, 456); System.Console.WriteLine(obj); // "456" What I would like to do is to write some IL