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
0
votes
3 answers
Function generation with arbitrary signature
Suppose you want to write a function "gen" that generates a function with a signature given by a list of types:
let gen (sig:Type list) = ....
let sig = [ typeof; typeof; typeof ]
let func = gen sig
then func is of type: int ->…

Daniel
- 1,522
- 1
- 12
- 25
0
votes
2 answers
Reference 'this' in dynamic event handler
In my 'myClass' class, I am using Reflection.Emit to dynamically write an event handler for one of the myClass class' members.
I have done this successfully.
Now, I want to modify the event handler to call one of the instance methods in the myClass…

emrosenf
- 87
- 5
-1
votes
1 answer
Reflection Emit to override ToString() in C#
Could someone help me to override ToString() method?
The C# code I want to build is as follows:
public override string ToString()
{
return $"{Id}, {Description}, {ParentId}";
}
The code I made to emit is as follows:
MethodBuilder…

Joon w K
- 777
- 1
- 6
- 27
-1
votes
1 answer
Why do I get System.InvalidProgramException: Common Language Runtime detected an invalid program
I'm just trying to generate code that just generate a int constant. Here is the code:
string name = Path.GetFileNameWithoutExtension(outputPath);
string filename = Path.GetFileName(outputPath);
AssemblyName assemblyName = new…

Lian
- 1
- 1
-1
votes
1 answer
How to call a method on a field with ILGenerator.Emit?
I want to know how to generator the following method myMethod with ilGenerator.
public class MyClass {
private MyField myField;
public int myMethod(int b) {
return myField.someMethod(b);
}
}
Can someone help me ?

user3265328
- 11
- 1
-1
votes
1 answer
Invalid IL code in XXX(): IL_0023: brfalse IL_00ba
I am trying to understand why this is invalid IL code and/or what would cause this fault.
The exception thrown is:
System.InvalidProgramException: Invalid IL code in
away3d.containers.View3D:updateBackBuffer (): IL_0023: brfalse
IL_00ba
I…

SushiHangover
- 73,120
- 10
- 106
- 165
-1
votes
1 answer
Is the Castclass OpCode necessary?
CLR allows me to set a field with an object reference regardless of whether a Castclass operation has been performed. Is it ok to set a field if there is certainty that the object reference on top of the stack is of the approperiate type? If so,…

toplel32
- 1,162
- 1
- 7
- 22
-1
votes
1 answer
Reflection: can't get a MethodInfo for 'Add' in class BindingList<> if the type argument is a TypeBuilder
We have a compiler that uses reflection emit to generate assemblies. We have stumbled with trying to obtain the MethodInfo for the Add method in the BindingList class, when T is a TypeBuilder object. We are using TypeBuilder.GetMethod(…

elchido
- 153
- 7
-1
votes
1 answer
Attaching an existing method to a dynamic assembly instead of generating IL for it
I am new to the Reflection.Emit API and have generated a bare minimum assembly with an entry point that returns an exit code based on a few simple I/O checks. The reason for creating an external assembly is that a piece of code needs to run outside…

Raheel Khan
- 14,205
- 13
- 80
- 168
-1
votes
2 answers
'System.Boolean' cannot be converted to type 'System.Reflection.RuntimePropertyInfo'
I have a class which is created dynamically .I have another existing class which has data so i am trying to map existing class data to to dynamicaly created class propeties.
But all the fileds in dynamic class are showing their types as…

Chandra Mohan
- 729
- 2
- 10
- 29
-1
votes
1 answer
Emiting MSIL to emit MSIL runs into JIT Limitation
So I wanted to answer https://codegolf.stackexchange.com/q/22921/12097 and decided to emit MSIL code to do the integer addition. Since this was successful, I then decided to emit MSIL code, which emits my first code. So calling the code constructs a…

John Alexiou
- 28,472
- 11
- 77
- 133
-1
votes
1 answer
Exception generated from destination of a call
I wrote this code:
AssemblyName assemblyName = new AssemblyName("SamAsm");
AssemblyBuilder assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
TypeBuilder typeBuilder =…

user1872492
- 93
- 1
- 8
-2
votes
1 answer
Is it possible to have implicit conversion operators being called during casts made by a method generated dynamically ? (eg: IL emit)
I have have the following classes A and B:
public class A
{
}
public class B
{
public static implicit operator A(B value)
{
return new A();
}
}
As expected, the following code compiles and runs fine (no exceptions):
A a…

tigrou
- 4,236
- 5
- 33
- 59
-2
votes
2 answers
PropertyBuilder for bool
I am stuck in a problem where i am trying to create a Boolean property at run time using Reflection.Emit. I am trying to reproduce the example Link Found Here For String Property , but I am failing to reproduce it for Boolean property, I am missing…

Anthony Semaan
- 60
- 7
-2
votes
1 answer
"Exception has been thrown by the target of an invocation." issue while implementing function with two params
I am trying to implement interface for dynamic generated class/method. The implementation is okay for method with one args(commented in code). But when method has two or more params it is throwing "Exception has been thrown by the target of an…

StartCoding
- 394
- 5
- 16