dynamicmethod

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

Call a method when a generic event occurs

若如初见. 提交于 2020-01-07 01:51:59
问题 I'm facing a problem when trying to implement a call to a method fired up by an event which should be defined during run-time. I found this answer: Redirecting to a dynamic method from a generic event handler and implemented that solution, but I keep getting an exception when the method to call is an instance one, not static. Here is my partial code: public class Operation { public bool EventFired { get { return _eventFired; } } private bool _eventFired = false; public void Execute(object

Call a method when a generic event occurs

自闭症网瘾萝莉.ら 提交于 2020-01-07 01:51:17
问题 I'm facing a problem when trying to implement a call to a method fired up by an event which should be defined during run-time. I found this answer: Redirecting to a dynamic method from a generic event handler and implemented that solution, but I keep getting an exception when the method to call is an instance one, not static. Here is my partial code: public class Operation { public bool EventFired { get { return _eventFired; } } private bool _eventFired = false; public void Execute(object

How to add Custom Attributes to a DynamicMethod-generated method?

别等时光非礼了梦想. 提交于 2020-01-05 03:27:08
问题 I was playing around with DynamicMethod and Expression Trees' Compilation (which uses DynamicMethod internally). I then wondered if there is a way to add a custom attribute to the generated method. I googled about it, but I couldn't find a way. I know that it's possible to do using CodeDom, but I want to use DynamicMethod . Someone mentioned Type Descriptor, but I'm not sure if it helps. Does anyone knows a way to define custom attributes to methods generated using DynamicMethod ? 回答1: No

Create DynamicMethod from Action<T> instructions

拜拜、爱过 提交于 2020-01-04 05:15:36
问题 I am playing around with DynamicMethod and aim to do the following: I have an Action from which I obtain the IL code as bytes using GetILAsByteArray() . From this bytes I would like to create a Dynamic method and execute is. Here an example of what I am trying to do: class Program { static void Main(string[] args) { //Create action and execute Action<string> myAction = s => { Console.WriteLine("Hello " + s); }; myAction("World"); //Get IL bytes byte[] ilBytes = myAction.GetMethodInfo()

DynamicMethod with generic type parameters

霸气de小男生 提交于 2019-12-29 07:40:06
问题 Is it possible to define a DynamicMethod with generic type parameters? The MethodBuilder class has the DefineGenericParameters method. Does the DynamicMethod have a counterpart? For example is it possible to create method with a signature like the one given blow using DynamicMethod? void T Foo<T>(T a1, int a2) 回答1: Actually there is a way, it's not exactly generic but you'll get the idea: public delegate T Foo<T>(T a1, int a2); public class Dynamic<T> { public static readonly Foo<T> Foo =

DynamicMethod with generic type parameters

风格不统一 提交于 2019-12-29 07:40:05
问题 Is it possible to define a DynamicMethod with generic type parameters? The MethodBuilder class has the DefineGenericParameters method. Does the DynamicMethod have a counterpart? For example is it possible to create method with a signature like the one given blow using DynamicMethod? void T Foo<T>(T a1, int a2) 回答1: Actually there is a way, it's not exactly generic but you'll get the idea: public delegate T Foo<T>(T a1, int a2); public class Dynamic<T> { public static readonly Foo<T> Foo =

prevent DynamicMethod VerificationException - operation could destabilize the runtime

删除回忆录丶 提交于 2019-12-24 04:06:48
问题 I am using IL generation to create a simple deserializer method which takes strings out of a Lucene document and sets properties or fields of a reference-type object (POCO). Whenever I try to run the generated method, I receive a VerificationException error. There are other questions regarding this error, a few of which have to do with DynamicMethods, but from what I can tell the issue I am having is different. operation-could-destablize-the-runtime-and-dynamicmethod-with-value-types msil

ILGenerator: How to use unmanaged pointers? (I get a VerificationException)

↘锁芯ラ 提交于 2019-12-21 16:25:45
问题 I'm making a sound synthesis program in wich the user can create his own sounds doing node-base compositing, creating oscillators, filters, etc... The program compiles the nodes onto an intermediary language wich is then converted onto an MSIL via the ILGenerator and DynamicMethod It works with an array in wich all operations and data are stored, but it will be faster if i was able to use pointers to allow me to do some bit-level operations later PD: Speed is very important!! I noticed that

Calling varargs method via DynamicMethod

一曲冷凌霜 提交于 2019-12-21 12:21:46
问题 I'm trying to call unmanaged printf-like function using DynamicMethod. At runtime I get a BadImageFormatException:Index not found. (Exception from HRESULT: 0x80131124) Is this a limitation of runtime or my emited code is wrong? public class Program { [DllImport("msvcrt40.dll",CallingConvention = CallingConvention.Cdecl)] public static extern int printf(string format, __arglist); static void Main(string[] args) { var method = new DynamicMethod("printf", typeof(void), new Type[0], true); var il