reflection.emit

Remove a dynamically generated type created by Reflection Emit

Hi, I am using reflection to emit some dynamic types at runtime. Why is not really important. My problem right now is that using classes like AssemblyBuilder, ModuleBuilder and TypeBuilder, a type is generated perfectly and I can work with it without a problem. However there can be instances where I might need to change the definition...

Using Reflection.Emit to emit a "using (x) { ... }" block?

I'm trying to use Reflection.Emit in C# to emit a using (x) { ... } block. At the point I am in code, I need to take the current top of the stack, which is an object that implements IDisposable, store this away in a local variable, implement a using block on that variable, and then inside it add some more code (I can deal with that last...

Reflection.Emit: How to convert MethodBuilder to RuntimeMethodInfo reliably?

After generating a type dynamically and calling TypeBuilder.CreateType, I want to create a delegate that points to a method in the new type. But if I use code like loadedType = typeBuilder.CreateType(); myDelegate = (MyDelegate)Delegate.CreateDelegate( typeof(MyDelegate), methodBuilder); Reusing the m...

In general, how to convert ilasm syntax into Reflection.Emit calls?

I am writing a special-purpose mini-compiler and I often view disassembled CIL to figure out how to do things. But it's often not obvious how to translate the disassembled code to Reflection.Emit calls. Does a reference manual exist or any other source of information for doing this translation? Edit: yes, mapping the opcodes to ILGenera...

copy an attribute of a property from one instance to another instance (of a different type ) at runtime

let's say I have 2 classes like this: public class Foo { [Required] public string Name {get;set;} } public class Bar { // put here [Required] at run-time public string Name {get;set;} } var foo = new Foo(); var bar = new Bar(); //copy the required from foo to bar is it possible to copy the attributes from foo to bar at run-time ? ...

What might cause this ExecutionEngineException?

I am trying to use Reflection.Emit to generate a wrapper class in a dynamic assembly. Automatic wrapper generation is part of a new open-source library I'm writing called "GoInterfaces". The wrapper class implements IEnumerable<string> and wraps List<string>. In C# terms, all it does is this: class List1_7931B0B4_79328AA0 : IEnumerable...

How to tell whether a class/method is accessible using reflection?

I use a Dynamic Assembly to create derived classes at run time. How can I tell, using reflection, whether the base class and individual methods in the base class can be used/called from within the derived class in the dynamic assembly? ...

How to create an x64 DynamicAssembly

I'm currently porting a project of mine from x86 to x64. It is a plugin and absolutely must run as an x64, since the host does too. Part of the application creates a dynamic assembly: AppDomain.CurrentDomain.DefineDynamicAssembly(...) And then saves that to the disk. I checked it with dumpbin /headers, its in x86! How can I force t...

What frameworks/languages support run-time class creation?

Hi. I'm trying to put together a list of frameworks/languages support run-time class creation. For example in .NET you can use the System.Reflection.Emit library to emit new classes at run time. If you could mention other frameworks/languages that support this (or some variation of it), that'd be really helpful. Thanks :) ...

System.AccessViolationException storing a variable with reflectio.emit

Hi, I'm building a compiler with reflection.emit in my spare time, and i've come to a problem that i'm not understanding. A little context, I've a runtime with a couple of types and one of them is Float2, a simpler vector struct with two float values (X and Y). I've made a couple of properties that allow me to swizzle the values (a la h...

DynamicMethod code unverifiable in .Net 4.0 (found ref 'this' pointer... expected ref '<>f__AnonymousType1`)

Hi All, Was using this solution to convert anonymous types to dictionaries using reflection.emit. Was working fine until I changed to .Net 4.0 from 3.5. Now, I'm getting the "System.Security.VerificationException: Operation could destabilize the runtime." error. Converted the anonymously loaded dynamic method to one hosted in a dyn...

Generate Multiple methods using Reflection

Hi, I would like to know how I can generate multiple type methods using Reflection. Example : class A() { public void CoreMethod1() { } public void CoreMethod2() { } // .. 20 such core methods public void Method1() { //some initializations //call to CoreMethod1(); } public void Method2() { //some initialization...

Serialize a list<string> and use each string as xml Node

I've ran into a problem and wondered if there's simple way of solving it. Here I have a XML template, defining some properties and their values. <Properties> <Property name="ID">10000</Property> <Property name="Name"> <SubProperty name="FirstName">Foo</SubProperty> <SubProperty name="LastName">Bar</SubProperty > </Propert...

Problem creating type with Reflection

Hello, I got a following base class: public class ValidationItem { public ObservableCollection<object> GetFilteredValues( ObservableCollection<object> values) { return new ObservableCollection<object>(); // nothing here yet } } I create a type which inherits this base type and I create a getter which is going to ...

Cloning/Copying get accessor body to new type

Hi, I'm creating new type in dynamic assembly from existing type, but with only selected properties to include: public class EmitTest { public Type Create(Type prototype, Type dynamicBaseType, List<string> includedPropertyList) { AssemblyName aName = new AssemblyName("DynamicAssembly"); AssemblyBuilder assemblyB...

Error with short form opcodes in Reflection.Emit

Hi. I'm making a small language that is very similar to hlsl but supports only pixel shaders. This language uses reflection.emit to build dot net assemblies that implement the same functionality. I'm currently testing my implementation of the branch instruction "if" and in one of my unit tests (a large if with inner if else's) failed wit...

Using Reflection.Emit to implement a interface

Let's say that I have the following interface: public interface IMyService { void SimpleMethod(int id); int Hello(string temp); } And want to generate a class that looks like this (using reflection emit). public class MyServiceProxy : IMyService { IChannel _channel; public MyServiceProxy(IChannel channel) { _channel...

How initialize and define const string with reflection.emit

i need help with .net reflection.emit Need create simple Assembly with public struct and string field in it. Field must be constant and i also need define it. In all i need get Assembly that hold inside something like this: namespace n { struct Alpha { public const string DATA = "Alpha"; } } I don't understand how cre...

Using "Method" MethodInfo property from an Action<T> delegate in il.EmitCall

Is something like this possible? // // create a delegate Action<Type> action = (t) => t.DoSomething; // // get the IL generator for a method ILGenerator il = myMethodBuilder.GetILGenerator(); // // try and call the delegate il.EmitCall(OpCodes.Callvirt, action.Method, null); Im getting a MethodAccessException whenever I try to invo...

How can in inject a literal expression using Reflection.Emit?

I am working on a project to evaluate tokenized user-defined expressions of varying complexity, using C# as the scripting language. I have a working model using CodeDOM and reflection to generate an evaluator class, create and load the assembly (GenerateInMemory = true), instantiate the class, and Execute the evaluate method. However, ...