reflection.emit

System.CodeDom.CodeCompileUnit from System.Reflection.Emit.AssemblyBuilder instance

I'm looking at CodePlex code for TLBImp2 and want to generate C# source code from the IL emitted. Is there a way to get a System.CodeDom.CodeCompileUnit from a System.Reflection.Emit.AssemblyBuilder instance? ...

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 dynamic IL into a dynamic assembly instead so I can save the generated IL into an assembly a...

Creating a dynamic enum and attempting to reference it fail with BindingFailure

I need to create a dynamic enum and then be able to get the type using Type.GetType(). Is this possible? The below code will create a dynamic enum, and attempt to use it's qualifying name. This is fine if I first store the assembly (using AssemblyBuilderAccess.RunAndSave). However, this is not possible if I'm solely using AssemblyBuilde...

Are there tools to ease the work with Reflection.Emit?

I need to dynamically generate some types. The generated type has several methods, constructors, implements an interface and defines the static constructor. Is there a tool that can help with the task? I am aware of this plugin for Reflector, yet it is only useful on the method level, while I have to generate a whole type. There is Run...

Linking a .NET Expression Tree into a new assembly

I'm trying to write my own toy My Toy Language -> MSIL compiler in order to get a better understanding of how compilers work. I got the parsing and lexing working, I have built the expression trees and using the System.Linq.Expressions expression tree API, I have a working interpreter. Now I would like to emit some real MSIL assemblies. ...

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<T>(string propertyName); } Does anyone have an example of how I would emit the following as a simple test case? class GeneratedObject : IObject { public T Get<T>(string propertyName) { // thi...

Explicit interface implementation and Reflection.Emit

Does anybody know how to implement an interface's property explicitly using Reflection.Emit? ...

Is it possible to invoke internal method from a dynamic method in .NET?

Dear ladies and sirs. I am trying to invoke an internal method from a dynamically generated one. The il code is simple: ldarg_0, callvirt, ret. Executing the method fails with TypeLoadException saying it cannot load the type on which the internal method is defined. When I think of it, this seems logical, because the dynamic method hos...

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 this ? ...

How to emit explicit interface implementation using reflection.emit?

Dear ladies and sirs. 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 = MethodAttributes.Private | MethodAttributes.Virtual | Meth...

Is it possible to make the transition from F(Type) to F<T> without reflection and without a dictionary?

Dear sirs and ladies. First, a little introduction. I have to functions: static class C { static void F1(Type type) { // Do something to invoke F2<T> } static void F2<T>() { // bla bla bla } } I wish to invoke F1(Type), which in turn should transition to the generic context pertinent to the given type parameter a...

Performance of dynamically created type

Using C#, I am noticing a significant difference in perfomance when populating a list with instances of a dynamically generated type versus a simple struct. The code below includes 4 different methods for populating a list with 100,000 objects. Each method performs differently: Button1: 15 milliseconds Button2: 31 milliseconds Button...

Reflection IL Code

Hi, I'm new to reflections. I need to create a class which inherits from a parent class. I need to create a readonly property. This property calls an existing function in the parent class by passing an argument 25. Everything works fine, except that I am unable to pass the value 25 to the function being called. Below is the code that ge...

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 = t.GetConstructor(new Type[] { }); ILGenerator ilgen = dm.GetILGenerator(); ilg...

C# Reflection: Emitting classes to existing assemblies

Is it possible to use Reflection.Emit to create types in an existing assembly, or do you need to define a new dynamic assembly to be able to contain dynamic types? Basically, I intend to read in one XML definition file which defines a class that is then instantiated multiple times and populated with the data from several other XML files...

How can I define two types referencing each other using IL Emit

I need to define something like this using reflection Emit: public class Foo { public Bar Bar { get; set; } } public class Bar { public Foo Foo { get; set; } } The difficulty is that when calling TypeBuilder.DefineProperty(), I need to pass the System.Type of the property's return value, which doesn't exist yet. If the refer...

Reason why MethodBuilder.DefineParameter could not set parameter name?

I'm creating an interface based on an existing interface for WCF concerns, but I have the "DefineParameter" not setting the parameter names (method parameters of the created type have no name). Can you see a reason why? public static Type MakeWcfInterface(Type iService) { AssemblyName assemblyName = new AssemblyName(Stri...

I'm attempting to write a .NET compiler using System.Reflection.Emit how do I do type resolution?

I've got a strategy for resolving types from referenced dlls. I'm stuck on trying to resolve types that are defined in the assembly that is being compiled. I'm using the System.Reflection.Emit apis with no 3rd party libraries. For instance: class A {} class B { public A AnInstanceOfA {get; private set;} } What's the best way to r...

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)? ...

How can I generate Virtual Properties with the AssemblyBuilder in C# 4.0?

I'm currently working on creating an Assembly with virtual properties. The examples on MSDN are only creating normal properties. How do I create a class inside an assembly which has virtual properties? I would like to be able to generate a class like this: public class ClassA { public virtual int Id { get; set; } ...