reflection.emit

Reflection-generated and generic types

不羁岁月 提交于 2020-01-23 04:44:36
问题 I'm having yet another nasty moment with Reflection.Emit and type management. Say, I have a type named MyType which is defined in the dynamically generated assembly. Calling MyType.GetMethods() results in a NotSupportedException , which has reduced me to writing my own set of wrappers and lookup tables. However, the same is happening when I'm calling GetMethods() or any other introspecting methods on standard generic types which use my own types as generic arguments: Tuple<int, string> =>

LambdaExpression CompileToMethod

非 Y 不嫁゛ 提交于 2020-01-21 12:17:29
问题 I Have a few lines of code public void CreateMethod<TContract>(Expression<Action<TContract>> method) { var innerMethod = Builder.DefineMethod("SomeName",MethodAttributes.Private); method.CompileToMethod(innerMethod); //more code } However the second line fails. I've tried with different versions of DefineMethod with little luck. Any suggestions? 回答1: Unfortunately, CompileToMethod requires a static method as its argument (see here). Therefore, you need to add MethodAttributes.Static to

Loading a ParameterInfo using IL Emit

微笑、不失礼 提交于 2020-01-15 08:55:12
问题 I am currently using Calling a method of existing object using IL Emit as a guide, and I can already do whatever is asked in question. Now, I have an attribute added to a parameter and I want to load that particular parameter's attribute so that I can call a method inside that attribute. I know it can be done by loading the MethodInfo and then getting the ParameterInfo and then getting attribute of that ParameterInfo in IL; I am simply trying to avoid writing that much IL. Is there a way to

Cloning/Copying get accessor body to new type

十年热恋 提交于 2020-01-14 14:13:29
问题 I'm creating new type in dynamic assembly from existing type, but with only selected properties to include: public class EmitTest { public Type Create(Type prototype, Type dynamicBaseType, List<string> includedPropertyList) { AssemblyName aName = new AssemblyName("DynamicAssembly"); AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly( aName, AssemblyBuilderAccess.RunAndSave); ModuleBuilder modulBuilder = assemblyBuilder.DefineDynamicModule(aName.Name, aName.Name +

Is it possible to invoke internal method from a dynamic method in .NET?

浪子不回头ぞ 提交于 2020-01-13 08:41:49
问题 I am trying to invoke an internal method from a dynamically generated one. The il code is simple: ldarg_0, callvirt, ret. Executing the method fails with TypeLoadException saying it cannot load the type on which the internal method is defined. When I think of it, this seems logical, because the dynamic method host assembly is not a friend of the method's declaring type assembly. However, I have expected the dynamic method still to work, just like Delegate.CreateDelegate works. After all, I

Using a Delegate to call a constructor

喜你入骨 提交于 2020-01-12 08:01:47
问题 I found this but tried to use it and failed. How can i create an object using reflections and make it fast by putting it in a delegate? DynamicMethod dm = new DynamicMethod("MyCtor", t, new Type[] { }); var ctor = t.GetConstructor(new Type[] { }); ILGenerator ilgen = dm.GetILGenerator(); ilgen.Emit(OpCodes.Ldarg_0); ilgen.Emit(OpCodes.Newobj, ctor); ilgen.Emit(OpCodes.Ret); var d = (Func<T>)dm.CreateDelegate(t); dm.Invoke(null, new object[] { }); Before putting it in a deleage i tried to at

Generate dynamic method to set a field of a struct instead of using reflection

孤者浪人 提交于 2020-01-09 09:07:31
问题 Let's say I have the following code which update a field of a struct using reflection. Since the struct instance is copied into the DynamicUpdate method, it needs to be boxed to an object before being passed. struct Person { public int id; } class Test { static void Main() { object person = RuntimeHelpers.GetObjectValue(new Person()); DynamicUpdate(person); Console.WriteLine(((Person)person).id); // print 10 } private static void DynamicUpdate(object o) { FieldInfo field = typeof(Person)

Reflection Emit: how to Convert Attribute instance to CustomAttributeBuilder or CustomAttributeData

删除回忆录丶 提交于 2020-01-01 11:58:30
问题 I made a generator class that build a proxy class based on interface which implement the interface. See my post on Build a Proxy class based on Interface without implementing it. I'm familiar with CustomAttributeData.GetCustomAttributes(MemberInfo target) , I used it when I read the Interface's members and succeed to import them to the proxy. I want to inject additional attributes to generated class in run-time. I'm asking for attributes instances to inject them into the proxy. For example: A

Feeding an object literal to ILGenerator

空扰寡人 提交于 2019-12-30 09:39:12
问题 Food obj = ...; ILGenerator gen = (...).GetILGenerator(); gen.Emit( ?? obj ?? ); // replace this gen.Emit(OpCodes.Call, typeof(Person).GetMethod("Eat")); It's apparently not possible to cleanly push obj onto the evaluation stack, but I am open to ugly hacks which might compromise e.g. portability. ModuleBuilder.DefineInitializedData allows one to store a System.Byte[] in the .sdata. Any ideas? Edit: the generated method is being emitted as part of a new assembly. 回答1: object o = ...; Func

ILGenerator: How to add boolean to the stack

倾然丶 夕夏残阳落幕 提交于 2019-12-30 08:20:08
问题 Here is the way I can put float value to the stack(in C#): ILGenerator gen = method.GetILGenerator(); gen.Emit(OpCodes.Ldc_R4, (float)12.5); How can I put boolean value to the stack by using Emit method? 回答1: There is no representation of a boolean value on the evaluation stack. The bool, char, byte, ushort, uint, and their signed variants are all represented as a 4-byte signed integer (i4). True: ldc.i4.1 False: ldc.i4.0 来源: https://stackoverflow.com/questions/1387010/ilgenerator-how-to-add