reflection.emit

Real world uses of Reflection.Emit

In all the books I've read on reflection they often say that there aren't many cases where you want to generate IL on the fly, but they don't give any examples of where it does make sense. After seeing Reflection.Emit as a job requirement for a gaming company I was curious where else it's being used. I'm now wondering if there are an...

How do I Emit a System.Linq.Expression?

I've got some code that generates various Func<> delegates using System.Linq.Expressions and Expression.Lambda<Func<>>.Compile() etc. I would like to be able to serialize the generated functions into an assembly for later use. In the past I've done some stuff with System.Reflection.Emit but now that Linq Expressions I'd rather not go tha...

Using Reflection.Emit to copy a custom attribute to another method

I am trying to generate a new set of wcf interfaces based on existing interfaces. I am using the Reflection.Emit namespace to accomplish this. My problem is how to copy the old custom attributes from one method to the new method. Every example I have seen of SetCustomAttributes() requires knowing the attribute type beforehand. I need ...

Reflection.Emit vs CodeDOM

What are some pros/cons for using the Reflection.Emit library versus CodeDOM for dynamically generating code at runtime? I am trying to generate some (relatively complicated) dynamic classes in a system based on metadata available at runtime in XML form. I will be generating classes that extend existing classes in the application assemb...

How can I emit a call to a delegate whose type is unfinished at the time of the emit?

Hi everyone. I'm having trouble emitting a call to a delegate whose type is unfinished at the time of the emit. I'll elaborate: I've declared the following delegate type: // Delegate type. The 'firstArgument' will be 'this', i.e., this is an open // instance method: the implicit argument is here given explicitly, in // 'firstArgument'. ...

NHibernate / Fluent NHibernate Dynamic Column Mapping

I have a table that, some of its columns are unknown at compile time. Such columns could either be of an integer value, or some Enum value. There is a table that holds all the names of such dynamic columns and also holds the column's type. This "metatable" has the following columns: DynamicColumnId (Pk) Name TypeId (Integer / Enum, as ...

Emitting a type deriving from another type and an interface with Reflection.Emit.

Dear ladies and sirs. I have a class A, which implements interface I: class A: I { // implementation of I } There is another interface J, which extends I: interface J : I { // J methods and properties. } I would like to emit a dynamic class B, which would look like so: class B : A, J { // All the constructors of A // All ...

How can I define multiple types with the same name and different type parameters using Reflection Emit?

How can I generate types like these using the System.Reflection.Emit libraries: public class Test<T> {} public class Test<T1, T2> {} When I call ModuleBuilder.DefineType(string) with the second type declaration, I get an exception because there is already another type in the module with the same name (I've already defined the type par...

Reflect.Emit Dynamic Type Memory Blowup

Using C# 3.5 I am trying to generate dynamic types at runtime using reflection emit. I used the Dynamic Query Library sample from Microsoft to create a class generator. Everything works, my problem is that 100 generated types inflate the memory usage by approximately 25MB. This is a completely unacceptable memory profile as eventually I ...

Using Reflection.Emit to match existing constructor

First, here is the C# code and the disassembled IL: public class Program<T> { private List<T> _items; public Program(T x, [Microsoft.Scripting.ParamDictionary] Microsoft.Scripting.IAttributesCollection col) { _items = new List<T>(); _items.Add(x); } } Here is the IL of that constructor: .method public...

Where can I find information on the Get, Set and Address methods for multidimensional System.Array instances in .NET?

System.Array serves as the base class for all arrays in the Common Language Runtime (CLR). According to this article: For each concrete array type, [the] runtime adds three special methods: Get/Set/Address. and indeed if I disassemble this C# code, int[,] x = new int[1024,1024]; x[0,0] = 1; x[1,1] = 2; x[2,2] = 3; Console.WriteLin...

Saving Types generated via Reflection.Emit as code file (.cs) instead of saving it in .dll files

Before start let me tell my experience: I am experienced with C#.NET, web services, XML part and few more. Reflection is something new to me, though I have read extensively on it and tried out some experimental code, but haven't made anything great using reflection I checked out many examples of how we can create Type at runtime and the...

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

Populating and Using Dynamic Classes in C#/.NET 4.0

In our application we're considering using dynamically generated classes to hold a lot of our data. The reason for doing this is that we have customers with tables that have different structures. So you could have a customer table called "DOG" (just making this up) that contains the columns "DOGID", "DOGNAME", "DOGTYPE", etc. Customer...

Replacing instructions in a method's MethodBody

Hi, (First of all, this is a very lengthy post, but don't worry: I've already implemented all of it, I'm just asking your opinion, or possible alternatives.) I'm having trouble implementing the following; I'd appreciate some help: I get a Type as parameter. I define a subclass using reflection. Notice that I don't intend to modify th...

What are the limitations of Reflection.Emit vs. other assembly generation techniques?

I've used Reflection.Emit in the past to write a compiler, but I know the standard compilers don't use it, and in an answer to another question here I saw a mention that there are some things Reflection.Emit is unable to do. What are the limitations of Reflection.Emit that I should be aware of if I plan on writing another compiler for ....

What's the maximum length of a type name in .NET

Possible Duplicate: What is the maximum length of a C#/CLI identifier? When generating dynamic assemblies and types, what is length restriction on the type name? ...

IL short-form instructions aren't short?

Hi. I was looking at the IL code of a valid method with Reflector and I've run into this: L_00a5: leave.s L_0103 Instructions with the suffix .s are supposed to take an int8 operand, and sure enough this should be the case with Leave_S as well. However, 0x0103 is 259, which exceeds the capacity of an int8. The method somehow works, bu...

Creating DescriptionAttribute on Enumeration Field using System.Reflection.Emit

I have a list of strings which are candidates for Enumerations values. They are Don't send diffs 500 lines 1000 lines 5000 lines Send entire diff The problem is that spaces, special characters are not a part of identifiers and even cannot start with a number, so I would be sanitizing these values to only chars, numbers and _ To keep...

How does LinqPad support WCF Data Services?

LinqPad supports WCF Data Services. If you assign an URL, such as http://services.odata.org/Northwind/Northwind.svc/. It will list all available data objects and you can query them. I guess LinqPad generates all available data classes at run time by reflection.Emit. I am wondering who can show me to how to do so. Or maybe someone has d...