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
9
votes
3 answers
Actual Performance of Fields vs. Properties
I'm doing some post-build CIL weaving that adds CIL to all methods in an assembly (in other words tons of methods). Each method checks if a specific value is null. Example (C# Reflector'd version of CIL code):
// CIL woven region start
if…

Jeff
- 35,755
- 15
- 108
- 220
9
votes
3 answers
Can Roslyn be used to generate dynamic method similar to DynamicMethod IL generation
I have been using DynamiMethod to generate the IL using
method.GetILGenerator();
This works well but is of course very hard to use since you generally don't want to work with low level IL in a high level language like C#. Now since there is Roslyn…

Ilya Chernomordik
- 27,817
- 27
- 121
- 207
9
votes
1 answer
Why do we need to explicitly call parent constructor in MSIL?
I just spent hours being confused by an NullReferenceException where I thought there shouldn't be one. I was constructing a class like so:
public class MyClass : MyBase
{
public MyClass()
{
base.Method(Foo.StaticField);
…

dav_i
- 27,509
- 17
- 104
- 136
9
votes
3 answers
How threadsafe is System.Reflection.Emit?
I'm wetting my feet with dynamic code generation and System.Reflection.Emit. All seems pretty easy and straightforward, but there's one question which I cannot find answered on the web.
When building dynamic assemblies with AssemblyBuilder it's…

Vilx-
- 104,512
- 87
- 279
- 422
9
votes
1 answer
Generating a Portable Class Library through Reflection.Emit
I am writing a compiler which generates on-disk .NET assemblies using the System.Reflection.Emit API. The compiler itself is built against .NET 4.5, but the generated code only references types from Portable Class Libraries. However, when trying to…

Trillian
- 6,207
- 1
- 26
- 36
9
votes
1 answer
Is it possible to invoke internal method from a dynamic method in .NET?
I am trying to invoke an internal method from a dynamically generated one. The il code is simple: ldarg_0, callvirt, ret.
Executing the method fails with TypeLoadException saying it cannot load the type on which the internal method is defined.
When…

mark
- 59,016
- 79
- 296
- 580
9
votes
2 answers
Cannot bind to the target method when creating delegates for properties
Trying to create two dictionaries of emitted delegates to allow for improved performance when dynamically getting/setting the values of properties.
Code:
Properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
…

Alex Hope O'Connor
- 9,354
- 22
- 69
- 112
9
votes
1 answer
Create a class dynamically with Reflection.Emit. I got stuck
A read about creating types at runtime and i found it amazing.
My goal is to create this class:
[DelimitedRecord(",")]
public class Person
{
[FieldOrder(0)]
private string firstName;
[FieldOrder(1)]
private string lastName;
…

gigi
- 3,846
- 7
- 37
- 50
9
votes
2 answers
Why is a TypeBuilder generated generic methodinfo not a generic method?
I have some code that uses a MethodInfo of a generic method found on a generated type. To avoid some reflection, I have the code use the
ldtoken Method
ldtoken Type
call GetMethodFromHandle(RuntimeMethodHandle,RunTimeTypeHandle)
Pattern to…

Michael B
- 7,512
- 3
- 31
- 57
9
votes
1 answer
Type.GetMethod alternative for TypeBuilder
I'm making a .NET-compliant compiler using Reflection.Emit. The problem is, that although TypeBuilder is derived from Type, it does not let me use all the handy methods that Type provides.
The matters that really concern me are:
Is there any way to…

Impworks
- 2,647
- 3
- 25
- 53
9
votes
1 answer
Dynamic Type Creation with a Constructor that reference its dependencies
I have the following classes:
public class Entity where T : Entity {
public Factory Factory { get; private set; }
public Entity(Factory factory) {
Factory = factory;
}
}
public class Factory { }
public class…

Rushui Guan
- 3,023
- 1
- 24
- 26
8
votes
5 answers
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…

Fritz H
- 3,539
- 2
- 26
- 37
8
votes
1 answer
Overriding property definitions with Reflection.Emit
I'm trying to implement this pattern using Reflection.Emit (TypeBuilder):
public class ClassToBeProxied
{
public virtual object Property1 { get; set; }
}
public class Proxy : ClassToBeProxied
{
[AttributeToBeAdded]
public override…

Joe Enzminger
- 11,110
- 3
- 50
- 75
8
votes
2 answers
DynamicMethod with generic type parameters
Is it possible to define a DynamicMethod with generic type parameters? The MethodBuilder class has the DefineGenericParameters method. Does the DynamicMethod have a counterpart? For example is it possible to create method with a signature like the…

Alex
- 2,040
- 2
- 19
- 29
8
votes
4 answers
Is it possible to indirectly load a value type on the stack
In Microsoft IL, to call a method on a value type you need an indirect reference. Lets say we have an ILGenerator named "il" and that currently we have a Nullable on top of the stack, if we want to check whether it has a value then we could emit the…

Greg Beech
- 133,383
- 43
- 204
- 250