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
8
votes
1 answer
Convert IntPtr to Int64: conv.u8 or conv.i8?
I'm working on an ILGenerator extension to help emit IL fragments using Expression. Everything was fine, until I worked on the integer conversion part. There are something really counter-intuitive to me, like:
Use conv.i8 to convert Int32 to…

kevinjwz
- 181
- 6
8
votes
1 answer
When implementing an interface that has a method with 'in' parameter by TypeBuilder.CreateType, TypeLoadException is thrown
Using TypeBuilder, I'm building a class that implements an interface that contains a method. After implementing that method with ILGenerator, then I call TypeBuilder.CreateType() and everything goes well in the normal case.
But if the method…

Codeption
- 141
- 8
8
votes
1 answer
C# Build anonymous object dynamically
In C# I can easily create an anonymous object like this at the compile time:
var items = new {
Price = 2000,
Description = "",
Locations = new List { "", "" }
};
My question is, it's possible to create this object at the runtime ?
I've…

amd
- 20,637
- 6
- 49
- 67
8
votes
1 answer
How can I define a global field in a ModuleBuilder
I'm making dynamic assemblies with System.Reflection.Emit, and I want to define a module-level field, the kind that can be retrieved with Module.GetField. There are methods in ModuleBuilder to define global methods, but the only thing I could find…

Anonymous
- 491
- 2
- 12
8
votes
2 answers
Create a copy of method from IL
I am trying to create a copy of a method during runtime using reflection.
I have the following code.
public static R CopyMethod(Func f, T t)
{
AppDomain currentDom = Thread.GetDomain();
AssemblyName asm = new AssemblyName();
…

Igor Ševo
- 5,459
- 3
- 35
- 80
8
votes
2 answers
Emit IL code to load a decimal value
I have code like this to emit IL code that loads integer or string values. But I don't know how to add the decimal type to that. It isn't supported in the Emit method. Any solutions to this?
ILGenerator ilGen = methodBuilder.GetILGenerator();
if…

ygoe
- 18,655
- 23
- 113
- 210
8
votes
1 answer
Why can't I step into a Call instruction during Debug / Disassembly?
The Disassembly looks like:
methShort( ref x, ref y );
000007FF00163F67 lea r8,[rsp+34h]
000007FF00163F6C lea rdx,[rsp+30h]
000007FF00163F71 mov rcx,qword ptr [rsp+20h]
000007FF00163F76 mov …

Veldaeven
- 319
- 1
- 13
8
votes
2 answers
What's an ansi class in C#?
I started using reflection in my project.
When I create a type and I want to specify the TypeAttributes, I have two options:
AnsiClass and Class. They are both set to 0 in the enum TypeAttributes.
public enum TypeAttributes
{
// LPTSTR is…

billybob
- 2,859
- 6
- 35
- 55
8
votes
6 answers
Replacing instructions in a method's MethodBody
(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…

Alix
- 927
- 7
- 21
8
votes
3 answers
How do I emit code and inject it into a loaded assembly?
I've built some Types dynamically using System.CodeDom.CodeCompileUnit, want to compile those into IL code in memory, and inject that IL code into an assembly loaded in memory - there is no need to save any of this to disk. Maybe my stated plan is…

John K
- 28,441
- 31
- 139
- 229
8
votes
1 answer
Have I made a mistake in this IL I'm not seeing?
I'm working on a compiler using System.Reflection.Emit, and I'm getting JIT limitation errors I can't figure out. The problem occurs in my implementation of function handles. I.e. generating the code for
function foo() { }
f = foo;
f();
Due to…

cthom06
- 9,389
- 4
- 36
- 28
8
votes
2 answers
Alternatives to Reflection.Emit for the Compact Framework
It seems that .NET CF is missing the very useful Reflection.Emit.
So far, I found this library as an alternative: http://www.codeplex.com/EmitCF.
However it seems to be an abandoned early version, so I'm looking for more options.
Does anyone know…

Hermit
- 1,214
- 13
- 24
8
votes
2 answers
Is it possible to use Reflection.Emit for the opcodes stelem.any and ldelem.any?
So, I recently did some experimenting and discovered that it appears that Reflection.Emit doesn't support all of the opcodes in the ECMA spec. There are 3 opcodes missing:
ldelem.any
stelem.any
no. (prefix)
Are these opcodes just not supported in…

Earlz
- 62,085
- 98
- 303
- 499
8
votes
4 answers
General purpose FromEvent method
Using the new async/await model it's fairly straightforward to generate a Task that is completed when an event fires; you just need to follow this pattern:
public class MyClass
{
public event Action OnCompletion;
}
public static Task…

Servy
- 202,030
- 26
- 332
- 449
8
votes
1 answer
Emitting delegate function call
I have the following C# code:
public static double f2(Func f, double x)
{
return f(x);
}
And here it's IL code:
.method public hidebysig static
float64 f2 (
class [mscorlib]System.Func`2 f,
…

Ivan Kochurkin
- 4,413
- 8
- 45
- 80