reflection.emit

Why does getting the mocked instance created with Moq throw a System.BadImageFormatException?

This question may be related to another question and it certainly results with a System.BadImageFormatException. Maybe it's the same thing but exposed differently? I have the following the code: public interface IFoo<T> where T : class, new() { T FooMethod(object o); } public interface IFooRepo { F GetFoo<T, F>() where T : class, ...

Is it possible to write an assembly which dynamically generates a new class and patches itself with the new class?

Is it possible to write an assembly which dynamically generates/emits a new class and patches itself to include the new class? How? ...

Using delegate/DLR Lambdas to override instance methods?

Hi all- In an effort to learn F# and .Net, I've been playing around with the to-be-released DLR. To that end, I've been playing around with reflection, in an effort to implement a basic type system that integrates nicely with the clr. While I'm able to instantiate a simple type that extends Object, I get an error when calling the me...

Dynamic C#.NET Webservice

I am using a class in a C# ASP.NET project to allow a script written in some random scripting language to expose webservice methods dynamically - in other words, the script should be able to expose a method of any name with any signature (as long as it's valid, anyway) to the outside world through this SOAP interface (able to add and rem...

Howto emit a delegate or lambda expression

Hello, I want to emit a method that returns a Func<>. Inside this method I have to create a delegate or a lambda expression which exactly serves the return type. Altogether it should look like this: // I have a resolve method that will be called inside my missing method // This is it's signature: object Resolve( params object[] args);...

PropertyInfo.AddValueChanged Equivalent for Fields???

I'm trying to find the equivalent of PropertyInfo.AddValueChanged for FieldInfo. I basically just need to receive an event any time the field's value changes. I'm assuming there's nothing like this and I'll have to manipulate IL code or something like that. I'm willing to go that route, but any suggestions on how I should go about it? An...

Replace Field with Property using System.Reflection.Emit namespace???

I'm trying to figure out a way to use the Reflection.Emit namespace to remove a field and replace it with my own Property implementation. I've been reading on how to generate code using Emit and writing and compiling IL code directly, but I haven't seen any examples on how to do a replace or remove using the Reflection.Emit namespace; Ca...

Reference 'this' in dynamic event handler

In my 'myClass' class, I am using Reflection.Emit to dynamically write an event handler for one of the myClass class' members. I have done this successfully. Now, I want to modify the event handler to call one of the instance methods in the myClass class. However, I cannot figure out how to push a reference to 'this' onto the MSIL sta...

Creating a class for an interface at runtime, in C#

I'm looking at taking a set of objects, let's say there's 3 objects alive at the moment, which all implement a common interface, and then wrap those objects inside a fourth object, also implementing the same interface. The fourth object's implementations of methods and properties would simply call the relevant bits on those 3 underlying...

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

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

two TypeBuilders calling eachother illegal?

Hey, I'm generating (using System.Reflection.Emit) two types: call them foo, bar. The catch is, foo instantiates and calls bar, and bar uses foo. All works fine when I create bar, but when I then start generating foo, I get typeloadexception saying that type foo could not be found. It happens (probably, as the error was vague) when I t...

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

DynamicMethod returns incorrect value when property type is Int64.

I'm working on a routine to use DynamicMethod to retrieve values from a object. It worked fine with most of data types, except for DateTime.Ticks, which is int64 In the following test app. I uses both MethodInfo and DynamicMethod, the methodInfo returns correct value but DynamicMethod doesn't. Any ideas? using System; using System.Re...

Is it possible to reflect a running .NET process?

Dear ladies and sirs. I have an application with dynamic types emitted at run-time. In order to inspect the generated .NET code, we save the dynamically generated assemblies and use the Reflector. I am wondering whether there is a way to reflect them the Reflector way while the respective process is running, without having to save th...

Alternatives to Reflection.Emit for the Compact Framework

It seems that .NET CF is missing the very useful Reflection.Emit. So far, I found this library as an alternative: http://www.codeplex.com/EmitCF. However it seems to be an abandoned early version, so I'm looking for more options. Does anyone know of another alternative to Emit? Or perhaps someone used EmitCF and can comment on its st...

Reflection.Emit to create dynamically the method

I'd like to create dynamically some method, which will accept single parameter - instance of class A and then will execute method B in passed instance of A. B has parameter of type int. So here is the schema: dynamicMethod(A a){ a.B(12); } Here what I tried: DynamicMethod method = new DynamicMethod(string.Empty, typeof(void), new[] {...

ILGenerator: How to add boolean to the stack

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

Reflection.Emit Generic Base Class Generic Method Call

I'm dynamically sub classing a generic type (filling it's contract) that has a generic method. I attempt to call this generic method but the assembly I produce has errors. Reflector crashes when trying to open the assembly and this code snippet does not run. The exception I get is: An attempt was made to load a program with an incorrec...

PEVerify Warning Parameter out of Sequence

I've built an assembly using Reflection.Emit. Running PEVerify returns 214 warnings of the same type: [MD]: Warning: Parameter out of sequence (parameter: 1; seq.num: 1). [token:0x06000171] Unfortunately there isn't much documentation around these sort of issues. I'm guessing because it's a metadata warning it's something to do with ...

Is it possible to emit a type deriving from a generic type while specifying itself as the generic type parameter?

Dear ladies and sirs. Imagine the following perfectly legal type hierarchy: class A<T> where T : A<T> { } class B : A<B> { public B():base(){} } My question is given a statically compiled definition of A<> is it possible to emit the type B dynamically? The problem is how to specify the parent type in ModuleBuilder.DefineType. Or...