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
8
votes
1 answer

ILGenerator.DeclareLocal() takes a type of a class not yet compiled

Toying with making a compiler for my own language, I'm trying to generate some MSIL code using the Reflection.Emit framework. It works fine when using int when I declare local variables. However, when I want to declare a local variable of a type I…
Carlo V. Dango
  • 13,322
  • 16
  • 71
  • 114
8
votes
2 answers

How to Emit event at runtime

I wish to generate (emit) class that implement an Interface at run-time with C#. I have succeeded to emit methods and properties, but I failed to emit an event. Here is the code I wish to emit: Namespace: TestInterfaces, Class:…
Ofir
  • 2,174
  • 1
  • 16
  • 15
7
votes
2 answers

Use Reflection to Build a Class (to build a dynamic FileHelper class)

Can I build a class as shown below dynamically using reflection? There are no methods, just public variables, some have custom attributes. Is the .Emit method required (from what I've seen, "Emit" looks a little challenging). I'm using software…
NealWalters
  • 17,197
  • 42
  • 141
  • 251
7
votes
2 answers

Getting types in mscorlib 2.0.5.0 (aka Silverlight mscorlib) via reflection on?

I am trying to add Silverlight support to my favorite programming langauge Nemerle. Nemerle , on compilation procedure, loads all types via reflection mainly in 2 steps 1-) Uses Assembly.LoadFrom to load assembly 2-) Usese Assembly.GetTypes() to…
7
votes
1 answer

Open Emitted assembly generated code appears empty in Reflector when it is not.

I'm generating a dynamic assembly using Reflection.Emit which includes a single class. I have a bug which is causing a BadImageException. To resolve this I need to see the compiled code, and therefore I'm saving the dynamic assembly to disk. I've…
TheCodeKing
  • 19,064
  • 3
  • 47
  • 70
7
votes
1 answer

Why setting a read only field with dynamic method causes error in this class?

One can store in a read only field of a class using strfld op code in dynamic method if it has its owner set to that class and JIT checks are turned off. An example is here. This approach, however, failed to work with the class that comes from F#,…
konrad.kruczynski
  • 46,413
  • 6
  • 36
  • 47
7
votes
3 answers

Is there kind of runtime C++ assembler library around?

For my small hobby project I need to emit machine code from C++ program in runtime. I have base address 0xDEADBEEF and want to write something like this: Assembler a((void*)0xDEADBEEF); a.Emit() << Push(Reg::Eax) << Push(Reg::Ebx) << …
Anton
  • 978
  • 5
  • 11
7
votes
3 answers

Where can I find information on the Get, Set and Address methods for multidimensional System.Array instances in .NET?

System.Array serves as the base class for all arrays in the Common Language Runtime (CLR). According to this article: For each concrete array type, [the] runtime adds three special methods: Get/Set/Address. and indeed if I disassemble this C#…
Rob Smallshire
  • 1,450
  • 1
  • 15
  • 22
7
votes
1 answer

Use Reflection.Emit to generate Types that reference each other

I want to generate Types via reflection at runtime that reference each other. With static code I would do this public class Order { public int Id { get; set; } public Customer Customer { get; set; } } public class Customer { public int…
Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
7
votes
4 answers

Using a Delegate to call a constructor

I found this but tried to use it and failed. How can i create an object using reflections and make it fast by putting it in a delegate? DynamicMethod dm = new DynamicMethod("MyCtor", t, new Type[] { }); var ctor =…
user34537
7
votes
2 answers

Using emitted type as type parameter in Reflection.Emit

[Name("Admin")] public class TestAdmin : TestUserBase { public TestAdmin(Type webDriverType) : base(webDriverType) { } } Currently, I have a bunch of classes of this form that I'd like to create at runtime using…
Matt Koster
  • 219
  • 1
  • 2
  • 8
7
votes
2 answers

Why does adding beforefieldinit drasticly improve the execution speed of generic classes?

I'm working on a proxy and for generic classes with a reference type parameter it was very slow. Especially for generic methods (about 400 ms vs 3200 ms for trivial generic methods that just returned null). I decided to try to see how it would…
Michael B
  • 7,512
  • 3
  • 31
  • 57
7
votes
2 answers

Using .NET's Reflection.Emit to generate an interface

I need to generate a new interface at run-time with all the same members as an existing interface, except that I will be putting different attributes on some of the methods (some of the attribute parameters are not known until run-time). How can it…
Matt Howells
  • 40,310
  • 20
  • 83
  • 102
7
votes
3 answers

Why does `OpCode.Value` have the "wrong" endianness?

Facts: The correct encoding for the CIL instruction rethrow's op-code is the two-byte sequence FE 1A. OpCodes.Rethrow.Value (which has type short) has value 0xFE1A on my little-endian machine. BitConverter honours the machine's endianness when…
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
6
votes
3 answers

Why isn't LocalBuilder.SetLocalSymInfo emitting variable names?

I tried running the sample code which appears on the documentation page for the System.Reflection.Emit.LocalBuilder class but it appears that the calls to LocalBuilder.SetLocalSymInfo(string, int, int) aren't doing anything since the IL Dissasembler…
Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69