Questions tagged [dynamicmethod]

DynamicMethod is a .Net class that can be used to define a method at runtime using CIL.

System.Reflection.Emit.DynamicMethod is a .Net class that can be used to define a method at runtime using .

115 questions
3
votes
1 answer

DynamicMethod call terminated due to a StackOverFlowException

I have this class (simplified example) public class Foo { public object Bar(Type type) { return new object(); } } and I want to call the Bar method on an instance of Bar using DynamicMethod as it is shown below: MethodInfo…
Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
3
votes
1 answer

Expression to mapping one object to another on same-properties

I'm trying to create a simple mapper using Expression by this code: public static class MyUtility { public static Action BuildMapAction(IEnumerable properties) { var sourceInstance =…
amiry jd
  • 27,021
  • 30
  • 116
  • 215
3
votes
1 answer

How to call Action from il generator

In this example code i am trying to invoke a anonymous action from il generator. I am not sure if and how i can load the reference to the delegate and how to call it. I can do it if the OnFunctionCall is a static method not property. public delegate…
ptp
  • 511
  • 5
  • 13
3
votes
1 answer

Why am I unable to create an instance of an object using IL Generation in C#?

I have the following class: private sealed class Person { public string Name { get; } public int Age { get; } public Person(string name) { Name = name; } public Person(string name, int age) { Name =…
MaYaN
  • 6,683
  • 12
  • 57
  • 109
3
votes
1 answer

Create DynamicMethod from Action instructions

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…
Sjoerd222888
  • 3,228
  • 3
  • 31
  • 64
3
votes
1 answer

Compile dynamic instance method by expression tree, with this, private and protected access?

Is it possible to create a dynamic method in C# (or possibly other .NET languages) as an instance method of an already existing type, with access to "this" reference, private and protected members? A legitimate access to private/protected members,…
Erik Hart
  • 1,114
  • 1
  • 13
  • 28
3
votes
1 answer

DynamicMethod call Instance method

If I create a DynamicMethod from inside a class method how can I call another method of my class from the DynamicMethod Delegate? I need somehow to capture the this reference in the DynamicMethod code. But I can't find an overladed version of…
AndiR
  • 179
  • 10
3
votes
1 answer

dynamic method invocation in expression tree

When constructing an expression tree, I have to use nodes invoking external methods in order to obtain values the expression could then continue evaluation with. These methods are supplied as Func and my code has no knowledge of where they…
3
votes
2 answers

DynamicMethod for ConstructorInfo.Invoke, what do I need to consider?

My question is this: If I'm going to build a DynamicMethod object, corresponding to a ConstructorInfo.Invoke call, what types of IL do I need to implement in order to cope with all (or most) types of arguments, when I can guarantee that the right…
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
3
votes
1 answer

How to create callback (add as dynamic argument, a function)?

I'm creating this method / function and I need to implement callback. I mean, I need to add as dynamic argument, a function. I have read several articles but I can not understand how to get it. Any idea or example of use? public void httpReq (final…
TyrionLannister
  • 103
  • 1
  • 3
  • 7
3
votes
0 answers

DynamicMethod and Func delegates

I have a class that looks like this: public class ClassWithFuncConstructor { public ClassWithFuncConstructor(Func func) { } } Now I want to use the DynamicMethod class to emit the code needed to create an instance of the…
seesharper
  • 3,249
  • 1
  • 19
  • 23
3
votes
1 answer

Why does this DynamicMethod (ldarg.1, newobj, ret) trigger a VerificationException?

I have this method which wraps a constructor in a dynamic factory method: static Func ToFactoryMethod(this ConstructorInfo ctor) where TResult : class { var factoryMethod = new DynamicMethod( name: …
3
votes
1 answer

Is it possible to call a DynamicMethod from MethodBuilder/ConstructorBuilder

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 =…
Sagi
  • 8,972
  • 3
  • 33
  • 41
2
votes
3 answers

Why is Ldvirtftn unverifiable?

Can anyone explain when using an anonymously hosted dynamic method why I get an unverifiable exception by ldvirtftn for a public virtual method on a public class? I set the following assembly level attributes as well: [assembly:…
Michael B
  • 7,512
  • 3
  • 31
  • 57
2
votes
1 answer

DynamicMethod Reflection Emit a call to a Func

I am figuring out Reflection.Emit for some internal libraries and got stuck with calling a Func passed in as an argument. My scenario test uses the code transferred to IL with Linqpad in the picture My code to duplicate the IL in a DynamicMethod is…
MiddleTommy
  • 359
  • 3
  • 10