Questions tagged [ilgenerator]
72 questions
45
votes
2 answers
Why is Calli Faster Than a Delegate Call?
I was playing around with Reflection.Emit and found about about the little-used EmitCalli. Intrigued, I wondered if it's any different from a regular method call, so I whipped up the code below:
using System;
using System.Diagnostics;
using…

user541686
- 205,094
- 128
- 528
- 886
27
votes
5 answers
How to mutate a boxed struct using IL
Imagine we have a mutable struct (yes, don't start):
public struct MutableStruct
{
public int Foo { get; set; }
public override string ToString()
{
return Foo.ToString();
}
}
Using reflection, we can take a boxed instance of…

Marc Gravell
- 1,026,079
- 266
- 2,566
- 2,900
14
votes
2 answers
Why is IL.Emit method adding additional nop instructions?
I have this code that emits some IL instructions that calls string.IndexOf on a null object:
MethodBuilder methodBuilder = typeBuilder.DefineMethod(
"Foo",
…

Selman Genç
- 100,147
- 13
- 119
- 184
14
votes
2 answers
Purpose of Emit.OpCodes in .NET for Windows Store apps API?
I am considering porting a third-party library to .NET for Windows Store apps. The library makes excessive use of System.Reflection.Emit.OpCodes via calls to the ILGenerator.Emit method overloads.
In the .NET for Windows Store Apps API, the OpCode…

Anders Gustafsson
- 15,837
- 8
- 56
- 114
13
votes
2 answers
Dynamically create type and call constructor of base-class
I need to create a class dynamically. Most things work fine but i'm stuck in generating the constructor.
AssemblyBuilder _assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyBuilder"), …

Daniel Bişar
- 2,663
- 7
- 32
- 54
12
votes
2 answers
ILGenerator catching exceptions doesn't work
I'm generating wrappers for types by using System.Reflection.Emit. At one point it's possible that the original object is throwing a error on access ( FaultException ) and the error should be catched by my try { } catch (Exception e) { } which i…

Felix K.
- 6,201
- 2
- 38
- 71
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
8
votes
3 answers
ILGenerator method inlining
Given following code:
using System;
using System.Reflection.Emit;
using System.Diagnostics;
using System.Reflection;
namespace ConsoleApplication1
{
class A
{
public int Do(int n)
{
return n;
}
}
…

user102808
- 321
- 1
- 2
- 9
8
votes
3 answers
Is there a good wrapper around ILGenerator?
I'm using System.Reflection.Emit for a while now, and find it (who don't?) as painful as bug prone.
Do you know if there is a good wrapper around the IL Generator, something that I can rely on to emit IL in a more safe and easier manner than with…

Romain Verdier
- 12,833
- 7
- 57
- 77
6
votes
1 answer
ILGenerator: How to use unmanaged pointers? (I get a VerificationException)
I'm making a sound synthesis program in which the user can create his own sounds doing node-base compositing, creating oscillators, filters, etc.
The program compiles the nodes onto an intermediary language which is then converted onto an MSIL via…

Rafael
- 2,642
- 2
- 24
- 30
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
Self-modifying C# (MSIL) code?
How can I dynamically modify MSIL code in-memory with C#/.NET?
And yes, I'm really looking to replace existing code that's already in memory on the fly.
Specifically, I'm not:
Looking to only generate new code (via expression templates,…

user541686
- 205,094
- 128
- 528
- 886
5
votes
1 answer
generics with IL?
Is it possible to use generics with the IL Generator?
DynamicMethod method = new DynamicMethod(
"GetStuff", typeof(int), new Type[] { typeof(object) });
ILGenerator il = method.GetILGenerator();
... etc

sgtz
- 8,849
- 9
- 51
- 91
5
votes
4 answers
transferring one object properties values to another one
Before all, I know about AutoMapper, and I don't want to use it. Because I'm learning C# and I want to receive a deep view of it. So I'm trying to do this issue (explained below) myself.
However, I'm trying to create a property copier to cope values…

amiry jd
- 27,021
- 30
- 116
- 215
5
votes
2 answers
Retrieve code from ILGenerator
I have write some function to create an exe file using ILGenerator. What I want is to show to user the IL language generated whithout using external tools like ILDasm or Reflector.
during the execution of my program I have added each OpCode to…

carlesh
- 537
- 1
- 4
- 17