dynamicmethod

Strange parameter sequence using Reflection.Emit

I have been looking at Reflection.Emit recently. I wrote a simple program that generates a DynamicMethod which simple calls another method with the same parameters class Program { static void Main(string[] args) { Program p = new Program(); p.Test(); } public delegate void TestHandler(int a, int b, int c...

NHibernate - Reflection or DynamicMethod ?

I have used NHibernbate in few projects and now learned about few more ORMs also. I understand that, NHibernate binds Class to Datalayer dynamically during runtime using the mapping file. My Question is , how this late binding is done ? I mean, which Methodology is used, 'Reflection' or 'DynamicMethod' ? In case, if it uses Reflection,...

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

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 ment...

DynamicMethod and out-parameters?

How do I define a DynamicMethod for a delegate that has an out-parameter, like this? public delegate void TestDelegate(out Action a); Let's say I simply want a method that sets the a argument to null when I call the method. Note that I know that a probably better way to handle this would be to make the method return the Action delega...

Curiosity: Why does Expression<...> when compiled run faster than a minimal DynamicMethod?

I'm currently doing some last-measure optimizations, mostly for fun and learning, and discovered something that left me with a couple of questions. First, the questions: When I construct a method in-memory through the use of DynamicMethod, and use the debugger, is there any way for me to step into the generated assembly code, when vie...

Stringbuilder in CIL (MSIL)

Hey there, I'm trying to generate code that takes a StringBuilder, and writes the values of all the properties in a class to a string. I've got the following, but I'm currently getting a "Invalid method token" in the following code: public static DynamicAccessor<T> CreateWriter(T target) //Target class to *serialize* { ...

Practical example of Dynamic method?

I want to learn dynamic method and its practical example using c#. Is there any relation between dynamic method and Reflection? Please help me. ...

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 type and number of arguments is going to be passed in before I make the call? Backgr...

Why does calling a DynamicMethod with an instance of my own class cause an exception?

I'm learning CIL by making my own functions at runtime with Reflection.Emit. I'm actually surprised how easy things have been up until now but I've hit something that I can't guess my way through and I can't find anything relative in the docs. I'm trying to create a function that simply prints a very simple class I have defined. If I ch...

How can I combine several Expressions into a fast method?

Suppose I have the following expressions: Expression<Action<T, StringBuilder>> expr1 = (t, sb) => sb.Append(t.Name); Expression<Action<T, StringBuilder>> expr2 = (t, sb) => sb.Append(", "); Expression<Action<T, StringBuilder>> expr3 = (t, sb) => sb.Append(t.Description); I'd like to be able to compile these into a method/delegate equi...

DynamicMethod NullReferenceException

Can anyone tell me what's wrong with my IL code here? IL_0000: nop IL_0001: ldarg.1 IL_0002: isinst MyXmlWriter IL_0007: stloc.0 IL_0008: ldloc.0 IL_0009: ldarg.2 IL_000a: ldind.ref IL_000b: unbox.any TestEnum IL_0010: ldfld Int64 value__/FastSerializer.TestEnum IL_0015: callvirt Void WriteValue(Int...

JIT Compiler error - Invalid Program Exception using Reflection.Emit

Can someone explain to me why the following works for the first test but throws an InvalidProgramException for the second test? I'm stumped. using System; using System.Reflection; using System.Reflection.Emit; namespace DMTest { class Program { static void Main(string[] args) { Test.NewResultWithPara...

How to pass value when subscribing to event and obtain it when the event is triggered (DynamicMethod usage problems)

The task is to create event handlers in runtime. I need the one method to be called with different parameter value for different events. The events and their number are only known in runtime. So I'm trying to generate dynamic methods, each of which will be assigned to some event, but in general they all just pass some value to an instanc...

How can I make my DynamicMethod security-critical?

I have a rather convoluted scenario where I want to create a DynamicMethod that's attached to a class in an in-memory AssemblyBuilder. The dynamic method calls a method "GetReplacement" in a (regular) assembly of mine. This worked fine in .NET 2.0, but in .NET 4.0 I get an error: MethodAccessException: Attempt by security transparent m...

c# + Using dynamicmethod with an attribute

[CustomAttribute] public bool IsGreen() { return true; } How could one write the above using a DynamicMethod in c#? UPDATE; per casperOne you cannot do this with a custom attribute. But what about a non-custom attribute such as: [Conditional("DEBUG")] public bool IsGreen() { return true; } Note: I created a new post, beca...

Catch events by dynamically adding Handler using Code Generation

I need to hook up all events in an application and trace all information (raising, arguments, etc.). I found this at Stackoverflow: http://stackoverflow.com/questions/2802774/tracing-all-events-in-vb-net However, I need a solution that not only writes the called event to console, but calls TraceVerbose with parameters showing sender a...