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
6
votes
0 answers
Generating a constructor that takes an argument and just calls base(argument) using TypeBuilder
I am trying to dynamically create an empty constructor that takes an argument and simply calls base(argument) using TypeBuilder.
My code is something like:
(...)
// Create a class derived from this class
TypeBuilder typeBuilder =…

Paull
- 1,020
- 1
- 12
- 21
6
votes
1 answer
emit Opcodes set field to a value
I am trying dynamic create a proxy, so im pleying with Emit.
So when I set my field with emit I also need to set a isDirty field boolan to true.
How can I do that ?
Property Customer
{
set
{
this.customerName = value;
this.isDirty =…

Dennis Larsen
- 461
- 4
- 12
6
votes
1 answer
Emitting `[MarshalAs(...)]` dynamically using Reflection.Emit
I need to create a dynamic .NET delegate with specially marshalled parameters (e.g. [MarshalAs(UnamangedType.LPWStr)]) at runtime using System.Reflection.Emit. This requires me to create a type inheriting MulticastDelegate (no problems so far), an…

unknown6656
- 2,765
- 2
- 36
- 52
6
votes
3 answers
Create DynamicMethod to assign value to a property?
This is a learning exercise. I created a method that takes a Foo and a string and sets the A property. I used the Reflector disassembly to make the following emit code. The disassembly looks like this:
.method private hidebysig static void…
user47589
6
votes
1 answer
How can I emit a dynamic method returning a ref?
I'm navigating the ins and outs of ref returns, and am having trouble emitting a dynamic method which returns by ref.
Handcrafted lambdas and existing methods work as expected:
class Widget
{
public int Length;
}
delegate ref int…
user6027203
6
votes
1 answer
How to unload a dynamic assembly created with RunAndCollect?
For performance optimization, I dynamically create new types per user request using the following code:
var dynamicAssemblyName = new AssemblyName(assemblyName);
AssemblyBuilder dynamicAssembly =…

Dejan
- 9,150
- 8
- 69
- 117
6
votes
6 answers
Add properties dynamically at run time in existing class c#
I have a UI from where we are adding following values in a table Fields
ProductName
ProductId
ProductCode
I have a existing class Product with some existing properties
public class Product
{
public string ProductID { get; set; }
…

amethianil
- 480
- 2
- 7
- 16
6
votes
6 answers
Runtime code injection using DynamicMethod?
Consider the following trivial code:
using System;
class Test
{
delegate int FooDelegate(int i);
FooDelegate Foo = FooImplementation;
static int FooImplementation(int i)
{
return i + 1;
}
public static void…

The Fiddler
- 2,726
- 22
- 28
6
votes
2 answers
Dynamically generated class that implements IEnumerator GetEnumerator() and IEnumerator IEnumerable.GetEnumerator()
I have a problem with Reflection.Emit. I want to have dynamically created class, that has simple implementation of ICollection. All methods I've defined fine, instead of next two: public IEnumerator GetEnumerator() & IEnumerator…

zabulus
- 2,373
- 3
- 15
- 27
6
votes
1 answer
How can in inject a literal expression using Reflection.Emit?
I am working on a project to evaluate tokenized user-defined expressions of varying complexity, using C# as the scripting language.
I have a working model using CodeDOM and reflection to generate an evaluator class, create and load the assembly…

Welton v3.62
- 2,210
- 7
- 29
- 46
6
votes
1 answer
How to write a C# class with Reflection.Emit dynamically according to IL
Suppose we have an interface:
public interface ICalculator
{
decimal Calculate(decimal x, decimal y);
}
the calculate logic is implemented in javascript (actually is TypeScript) code, we want to dynamically create the follow implementation…

jason bie
- 71
- 3
6
votes
1 answer
Calling varargs method via DynamicMethod
I'm trying to call unmanaged printf-like function using DynamicMethod. At runtime I get a
BadImageFormatException:Index not found. (Exception from HRESULT:
0x80131124)
Is this a limitation of runtime or my emited code is wrong?
public class…

user629926
- 1,910
- 2
- 18
- 43
6
votes
1 answer
IL short-form instructions aren't short?
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.…

Alix
- 927
- 7
- 21
6
votes
1 answer
Fastest way for Get Value of a property (Reflection) in C#
I want to know what is fastest way to get value (only for this problem) from an object`s property ?
after some searching I saw a post from @MarkGravell in this site
He wrote this code :
using System;
using System.Reflection;
using…

user3153878
- 421
- 1
- 4
- 6
6
votes
2 answers
IL Emit for invoking a delegate instance?
Basically, I'm accepting an event name as a string, to get the EventInfo. Then, I'm discovering the event handler type and event argument type using reflection, creating a new delegate of that type (myEventHandler), and hooking it up with the event.…

amazedsaint
- 7,642
- 7
- 54
- 83