The System.Reflection.Emit namespace contains classes that allow a compiler or tool to emit metadata and Microsoft intermediate language (MSIL) and optionally generate a PE file on disk.
Questions tagged [reflection.emit]
737 questions
12
votes
3 answers
How to Apply XmlIncludeAttribute to TypeBuilder?
I am developing a library in C# that generates runtime types using System.Reflection.Emit.TypeBuilder class and i want to generate the following class hierarchy:
[XmlInclude(typeof(Derived))]
public class Base
{
}
public class Derived : Base
{
}
I…

Timothy Ghanem
- 1,606
- 11
- 20
12
votes
2 answers
How to specify Namespace for a type created through Reflection.Emit?
Apologies if I'm missing something obvious, but when I create a new type with Reflection.Emit, how do I specify what namespace it should be in?
ie..
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = "TestDynamic";
AssemblyBuilder…

Binary Worrier
- 50,774
- 20
- 136
- 184
12
votes
1 answer
How to create a method at runtime using Reflection.emit
I'm creating an object at runtime using reflection emit. I successfully created the fields, properties and get set methods.
Now I want to add a method. For the sake of simplicity let's say the method just returns a random number. How do I define the…

Dan Schubel
- 279
- 1
- 5
- 14
12
votes
1 answer
Difference between IKVM.Reflection.Emit and Mono.Cecil
IKVM.Reflection.Emit has "the ability to read and emit .NET 1.1, .NET 2.0 and .NET 4.0 assemblies (while running on, for example, .NET 2.0).".
Does Mono.Cecil have the same? Are they interchangable for this use case? Are they both supported…

Vlad
- 3,001
- 1
- 22
- 52
12
votes
4 answers
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…

Firestrand
- 1,305
- 10
- 19
12
votes
1 answer
Emit local variable and assign a value to it
I'm initializing an integer variable like this:
LocalBuilder a = ilGen.DeclareLocal(typeof(Int32));
How can I access it and assign a value to it? I want to do something like this:
int a, b;
a = 5;
b = 6;
return a + b;

Seishin
- 1,493
- 1
- 19
- 30
11
votes
2 answers
Using Reflection.Emit to emit a "using (x) { ... }" block?
I'm trying to use Reflection.Emit in C# to emit a using (x) { ... } block.
At the point I am in code, I need to take the current top of the stack, which is an object that implements IDisposable, store this away in a local variable, implement a using…

Lasse V. Karlsen
- 380,855
- 102
- 628
- 825
11
votes
1 answer
Dynamically creating an assembly targeting a specific .NET runtime using Reflection.Emit
I'm using Reflection.Emit to develop a tool that dynamically creates an Assembly at runtime.
The tool is targeting the .NET 4.5 framework.
I'd like to know if it's possible to specify which .NET runtime the dynamically generated assembly targets…

lysergic-acid
- 19,570
- 21
- 109
- 218
11
votes
1 answer
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?

Pavel Podlipensky
- 8,201
- 5
- 42
- 53
11
votes
6 answers
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…

Buu
- 49,745
- 5
- 67
- 85
11
votes
2 answers
Is it possible to add a method to an EXISTING class at runtime? why or why not?
I would imagine this might use Reflection.Emit,
but a similar question on SO only answers how to create a class/method dynamically, not how to update an existing class.
In a similar vein, is it possible to delete methods / classes at runtime? If…

user420667
- 6,552
- 15
- 51
- 83
10
votes
1 answer
Reflection.Emit.ILGenerator Exception Handling "Leave" instruction
First, some background info:
I am making a compiler for a school project. It is already working, and I'm expending a lot of effort to bug fix and/or optimize it. I've recently run into a problem with is that I discovered that the ILGenerator object…

leviathanbadger
- 1,682
- 15
- 23
10
votes
3 answers
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…

Lasse V. Karlsen
- 380,855
- 102
- 628
- 825
9
votes
4 answers
Reflection.Emit - access topmost-but-one item from stack
Is there a way in .NET, using Reflection.Emit, to access the topmost-but-one item from the stack? So if A is topmost, and B next - I want to process B then A. It would be fine to duplicate B above A (since I can simply "pop" the second B when I get…

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
9
votes
1 answer
Expression.DebugInfo How Do I Tag Expressions?
So I know what Expression.DebugInfo is used for, and I have an Debug expression created, but how to I tag other expressions with this debug info? Here's what I'm trying as a really basic test:
using System;
using…

Timothy Baldridge
- 10,455
- 1
- 44
- 80