Questions tagged [reflection.emit]

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.

737 questions
262
votes
16 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
  • 14,601
  • 23
  • 89
  • 112
65
votes
6 answers

Call and Callvirt

What is the difference between the CIL instructions "Call" and "Callvirt"?
Eric Smith
  • 5,262
  • 2
  • 33
  • 49
53
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
  • 129,300
  • 32
  • 216
  • 265
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
  • 380,855
  • 102
  • 628
  • 825
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
44
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,641
  • 10
  • 65
  • 98
40
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
  • 2,061
  • 3
  • 18
  • 26
39
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…
29
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
  • 59,016
  • 79
  • 296
  • 580
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
  • 353,942
  • 115
  • 666
  • 851
21
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
20
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
  • 23,796
  • 16
  • 59
  • 82
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,442
  • 17
  • 30
17
votes
5 answers

Reflection.Emit better than GetValue & SetValue :S

I've been told to use Reflection.Emit instead of PropertyInfo.GetValue / SetValue because it is faster this way. But I don't really know what stuff from Reflection.Emit and how to use it to substitute GetValue and SetValue. Can anybody help me with…
Omu
  • 69,856
  • 92
  • 277
  • 407
16
votes
1 answer

What are dynamic methods and how is DynamicMethod different from MethodBuilder?

I've come across dynamic methods a little in reflection-based C# code, and I'm yet to figure out precisely what they are. There specifically seems to be a DynamicMethod class that allows the generation and specification of CLR methods at runtime.…
Noldorin
  • 144,213
  • 56
  • 264
  • 302
1
2 3
49 50