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]
704 questions
241
votes
15 answers
How to dynamically create a class?
I have a class which looks like this:
public class Field
{
public string FieldName;
public string FieldType;
}
And an object List with values:
{"EmployeeID","int"},
{"EmployeeName","String"},
{"Designation","String"}
I want to…
ashwnacharya
- 13,821
- 22
- 82
- 111
61
votes
6 answers
Call and Callvirt
What is the difference between the CIL instructions "Call" and "Callvirt"?
Eric Smith
- 4,782
- 1
- 29
- 45
53
votes
1 answer
Curiosity: Why does Expression<...> when compiled run faster than a minimal DynamicMethod?
I'm currently doing some last-measure optimizations, mostly for fun and learning, and discovered something that left me with a couple of questions.
First, the questions:
When I construct a method in-memory through the use of DynamicMethod, and use…
Lasse V. Karlsen
- 350,178
- 94
- 582
- 779
52
votes
3 answers
Reflection.Emit vs CodeDOM
What are some pros/cons for using the Reflection.Emit library versus CodeDOM for dynamically generating code at runtime?
I am trying to generate some (relatively complicated) dynamic classes in a system based on metadata available at runtime in XML…
LBushkin
- 121,016
- 31
- 208
- 258
44
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
- 189,354
- 112
- 476
- 821
42
votes
17 answers
Real world uses of Reflection.Emit
In all the books I've read on reflection they often say that there aren't many cases where you want to generate IL on the fly, but they don't give any examples of where it does make sense.
After seeing Reflection.Emit as a job requirement for a…
Ryu
- 8,357
- 8
- 59
- 98
38
votes
1 answer
How to emit a Type in .NET Core
In C#, how do I emit a new Type at runtime with .NET Core? All of the examples I can find for .NET 6 don't seem to work in .NET core (they all begin with getting the current AppDomain, which doesn't exist in .NET core any more).
If possible I would…
ThomYorkkke
- 1,891
- 2
- 14
- 24
36
votes
6 answers
Fast creation of objects instead of Activator.CreateInstance(type)
I'm trying to improve the performance of our application. We have a lot of Activator.CreateInstance calls that are causing some grief.
We instantiate a lot of classes based on an interface (ITabDocument) and after looking around I thought of using…
Tiffany Townsend
- 361
- 1
- 3
- 5
27
votes
6 answers
Creating method dynamically, and executing it
Background:
I want to define few static methods in C# , and generate IL code as byte array, from one of these methods, selected at runtime (on client), and send the byte array over network to another machine (server) where it should be executed…
Nawaz
- 327,095
- 105
- 629
- 812
27
votes
1 answer
How to emit explicit interface implementation using reflection.emit?
Observe the following simple source code:
using System;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
namespace A
{
public static class Program
{
private const MethodAttributes ExplicitImplementation…
mark
- 49,076
- 65
- 227
- 485
19
votes
2 answers
Multiple types in one dynamic assembly is way slower than multiple dynamic assemblies with one type each
So I'm emitting some dynamic proxies via DefineDynamicAssembly, and while testing I found that:
One type per dynamic assembly: fast, but uses a lot of memory
All types in one dynamic assembly: very very slow, but uses much less memory
In my test I…
Chris
- 5,257
- 12
- 28
19
votes
2 answers
Java Equivalent of Reflection.Emit
As far as I can tell, Java has no such equivalent of C#'s Reflection.Emit stuff. Are there any additional libraries for Java that provide similar functionality? What are the differences (to reflection emit)?
PythonPower
17
votes
2 answers
Why are PropertyInfo SetValue and GetValue so slow?
Why is the PropertyInfo methods for getting and setting a property so slow? If I build a delegate using Reflection.Emit, it is much faster.
Are they doing something important, so that the time they take can be justified? That is... am I missing…
Miguel Angelo
- 22,641
- 14
- 53
- 80
16
votes
1 answer
Why am I getting this exception when emitting classes that reference each other via value-type generics?
This code snippet is a simplified extract of my class-generation code, which creates two classes that reference each other as arguments in a generic type:
namespace Sandbox
{
using System;
using System.Reflection;
using…
FacticiusVir
- 1,977
- 14
- 27
16
votes
3 answers
How do I Emit a System.Linq.Expression?
I've got some code that generates various Func<> delegates using System.Linq.Expressions and Expression.Lambda>.Compile() etc. I would like to be able to serialize the generated functions into an assembly for later use. In the past I've done…
dkackman
- 14,361
- 11
- 63
- 118