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
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
  • 2,037
  • 15
  • 28
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
  • 15,179
  • 13
  • 69
  • 123
16
votes
6 answers

Modifying Existing .NET Assemblies

Is there a way to modify existing .NET assemblies without resorting to 3rd party tools? I know that PostSharp makes this possible but I find it incredibly wasteful that the developler of PostSharp basically had to rewrite the functionality of the…
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
14
votes
2 answers

Why is IL.Emit method adding additional nop instructions?

I have this code that emits some IL instructions that calls string.IndexOf on a null object: MethodBuilder methodBuilder = typeBuilder.DefineMethod( "Foo", …
Selman Genç
  • 100,147
  • 13
  • 119
  • 184
14
votes
4 answers

How do I add attributes to a method at runtime?

We're using Microsoft.Practices.CompositeUI.EventBroker to handle event subscription and publication in our application. The way that works is that you add an attribute to your event, specifying a topic name, like this: [EventPublication("example",…
Simon
  • 25,468
  • 44
  • 152
  • 266
14
votes
5 answers

Using Reflection.Emit to create a class implementing an interface

I need to generate a class using Reflection.Emit that implements the following interface. public interface IObject { T Get(string propertyName); } Does anyone have an example of how I would emit the following as a simple test case? class…
karma
  • 141
  • 1
  • 1
  • 3
14
votes
2 answers

Reflection-generated and generic types

I'm having yet another nasty moment with Reflection.Emit and type management. Say, I have a type named MyType which is defined in the dynamically generated assembly. Calling MyType.GetMethods() results in a NotSupportedException, which has reduced…
Impworks
  • 2,647
  • 3
  • 25
  • 53
14
votes
2 answers

Purpose of Emit.OpCodes in .NET for Windows Store apps API?

I am considering porting a third-party library to .NET for Windows Store apps. The library makes excessive use of System.Reflection.Emit.OpCodes via calls to the ILGenerator.Emit method overloads. In the .NET for Windows Store Apps API, the OpCode…
Anders Gustafsson
  • 15,837
  • 8
  • 56
  • 114
13
votes
5 answers

C# Reflection - How to set field value for struct

How can I set value into struct field - myStruct.myField with reflection using DynamicMethod? When I call setter(myStruct, 111) value was not set, because MyStruct is value type. Console.WriteLine(myStruct.myField) shows value 3. How to modify…
Cyrus
  • 2,261
  • 2
  • 22
  • 37
13
votes
1 answer

Creating dynamic type from TypeBuilder with a base class and additional fields generates an exception

I am trying to create a dynamic type based on an existing type that contains only public fields. The new dynamic type must also inherit from a different base type which only has a fully implemented method. I create the TypeBuilder specifying the…
dtaylor
  • 997
  • 3
  • 10
  • 26
13
votes
2 answers

What's faster: expression trees or manually emitting IL

Is there a performance difference between creating a method emitting IL directly, as opposed to building an expression tree?
sircodesalot
  • 11,231
  • 8
  • 50
  • 83
13
votes
2 answers

Is it possible to skip visibility checks when generating dynamic IL with MethodBuilder's?

When generating IL using DynamicMethod it's possible to call methods and access fields that would be otherwise un-accessible if you provide 'true' for the restrictedSkipVisibility parameter in the DynamicMethod constructor I would prefer to emit…
Brandon Cuff
  • 1,438
  • 9
  • 24
13
votes
3 answers

C# reflection: If ... else?

I'm currently facing new problem with operators. Using following code, I want to make output that would be same as when using if ... else pair in C#. var method = new DynamicMethod("dummy", null, Type.EmptyTypes); var g =…
user35443
  • 6,309
  • 12
  • 52
  • 75
13
votes
2 answers

Dynamically create type and call constructor of base-class

I need to create a class dynamically. Most things work fine but i'm stuck in generating the constructor. AssemblyBuilder _assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("MyBuilder"), …
Daniel Bişar
  • 2,663
  • 7
  • 32
  • 54
12
votes
2 answers

ILGenerator catching exceptions doesn't work

I'm generating wrappers for types by using System.Reflection.Emit. At one point it's possible that the original object is throwing a error on access ( FaultException ) and the error should be catched by my try { } catch (Exception e) { } which i…
Felix K.
  • 6,201
  • 2
  • 38
  • 71
1
2
3
49 50