reflection.emit

Is it possible to call a DynamicMethod from MethodBuilder/ConstructorBuilder

拈花ヽ惹草 提交于 2021-02-09 08:55:22
问题 I have an ILGenerator created from ConstructorBuilder, and I want to create and call a DynamicMethod with it but I get an InvalidOperationException - Unable to import a global method or field from a different module. var constructorBuilder = typeBuilder.DefineConstructor(...); var ilGenFromCtor = constructorBuilder.GetILGenerator(); . . . var dynamicMethod = new DynamicMethod("Name", ReturnType, Type.EmptyTypes, true); var ilGenFromDynamicMethod = dynamicMethod.GetILGenerator(); . . var

Why am I getting this exception when emitting classes that reference each other via value-type generics?

眉间皱痕 提交于 2021-02-06 08:43:59
问题 This code snippet is a simplified extract of my class-generation code, which creates two classes that reference each other as arguments in a generic type: namespace Sandbox { using System; using System.Reflection; using System.Reflection.Emit; internal class Program { private static void Main(string[] args) { var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("Test"), AssemblyBuilderAccess.Run); var module = assembly.DefineDynamicModule("Test"); var typeOne = module

Create a copy of method from IL

老子叫甜甜 提交于 2021-01-27 03:50:39
问题 I am trying to create a copy of a method during runtime using reflection. I have the following code. public static R CopyMethod<T, R>(Func<T, R> f, T t) { AppDomain currentDom = Thread.GetDomain(); AssemblyName asm = new AssemblyName(); asm.Name = "DynamicAssembly"; AssemblyBuilder abl = currentDom.DefineDynamicAssembly(asm, AssemblyBuilderAccess.Run); ModuleBuilder mbl = abl.DefineDynamicModule("Module"); TypeBuilder tbl = mbl.DefineType("Type"); var info = f.GetMethodInfo(); MethodBuilder

Fastest way for Get Value of a property (Reflection) in C#

江枫思渺然 提交于 2020-06-25 03:50:33
问题 I want to know what is fastest way to get value (only for this problem) from an object`s property ? after some searching I saw a post from @MarkGravell in this site He wrote this code : using System; using System.Reflection; using System.Reflection.Emit; public class Foo { public Foo(int bar) { Bar = bar; } private int Bar { get; set; } } static class Program { static void Main() { var method = new DynamicMethod("cheat", typeof(int), new[] { typeof(object) }, typeof(Foo), true); var il =

Fastest way for Get Value of a property (Reflection) in C#

拥有回忆 提交于 2020-06-25 03:50:08
问题 I want to know what is fastest way to get value (only for this problem) from an object`s property ? after some searching I saw a post from @MarkGravell in this site He wrote this code : using System; using System.Reflection; using System.Reflection.Emit; public class Foo { public Foo(int bar) { Bar = bar; } private int Bar { get; set; } } static class Program { static void Main() { var method = new DynamicMethod("cheat", typeof(int), new[] { typeof(object) }, typeof(Foo), true); var il =

Emitting delegate function call

♀尐吖头ヾ 提交于 2020-02-20 07:39:21
问题 I have the following C# code: public static double f2(Func<double, double> f, double x) { return f(x); } And here it's IL code: .method public hidebysig static float64 f2 ( class [mscorlib]System.Func`2<float64, float64> f, float64 x ) cil managed { // Method begins at RVA 0x20bd // Code size 8 (0x8) .maxstack 8 IL_0000: ldarg.0 IL_0001: ldarg.1 IL_0002: callvirt instance !1 class [mscorlib]System.Func`2<float64, float64>::Invoke(!0) IL_0007: ret } How can I to emit callvirt instance !1 class

Reflection.Emit - access topmost-but-one item from stack

╄→尐↘猪︶ㄣ 提交于 2020-02-20 07:22:21
问题 Is there a way in .NET, using Reflection.Emit , to access the topmost-but-one item from the stack? So if A is topmost, and B next - I want to process B then A. It would be fine to duplicate B above A (since I can simply "pop" the second B when I get to it). Currently, I am declaring a local: LocalBuilder loc = il.DeclareLocal(typeof(Foo)); il.Emit(OpCodes.Stloc, loc); // store and pop topmost stack item // work with (pop) previous stack item il.Emit(OpCodes.Ldloc, loc); // push old topmost

Convert from Type to TypeBuilder

我的未来我决定 提交于 2020-01-26 01:16:50
问题 I'm creating a class on runtime and some of the types are already created inside the ModuleBuilder and I would like to reused them, but I only have the Type and not the TypeBuilder (Which is what I need in order to change it) Is there a way to convert from Type to TypeBuilder? Type moduleType = ModuleBuilder.GetType(inXmlTemplateProperty.PropertyName); if (moduleType == null) { TypeBuilder newClass = ModuleBuilder.DefineType(inXmlTemplateProperty.PropertyName, TypeAttributes.Public |