reflection.emit

Is it possible to indirectly load a value type on the stack

In Microsoft IL, to call a method on a value type you need an indirect reference. Lets say we have an ILGenerator named "il" and that currently we have a Nullable on top of the stack, if we want to check whether it has a value then we could emit the following: var local = il.DeclareLocal(typeof(Nullable<int>)); il.Emit(OpCodes.Stloc, lo...

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 whole System.Reflection namespace in order to make existing assemblies modifiable. System.R...

Using .Net's Reflection.Emit to generate 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). Can anyone assist? ...

Assembly not saving correctly

I have some very simple code to generate an assembly and invoke a method on a contained type. The method gets called and runs correctly, however when I view the generated assembly using Reflector, I don't see the type. Below is the sample code: namespace ConsoleApplication2 { class Proggy { public static void Main(string[...

Call and Callvirt

What is the difference between the CIL instructions "Call" and "Callvirt"? ...

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", PublicationScope.Global)] public event EventHandler Example; then you add another attr...

What's the MSIL to call a base class's event handler?

I have a class called EventConsumer which defines an event EventConsumed and a method OnEventConsumed as follows: public event EventHandler EventConsumed; public virtual void OnEventConsumed(object sender, EventArgs e) { if (EventConsumed != null) EventConsumed(this, e); } I need to add attributes to the at OnEventConsume...

How fast is AppDomain creation?

I have just discovered that if generating assemblies via Reflection.Emit, the .NET framework keeps references in a static member that prevents Reflection.Emit classes not to be GC'ed. I cannot use DynamicMethod due to limitations. I also generate a lot of assemblies (incremental compiler of IronScheme) over the course of a program (co...

Strange parameter sequence using Reflection.Emit

I have been looking at Reflection.Emit recently. I wrote a simple program that generates a DynamicMethod which simple calls another method with the same parameters class Program { static void Main(string[] args) { Program p = new Program(); p.Test(); } public delegate void TestHandler(int a, int b, int c...

Finding all assembly dependencies, Reflector style

Hello everyone, I am generating an Assembly on the fly using Reflection.Emit and then saving it. It contains one Type and a static Main() method in it. .NET is kind enough to automatically reference a required assembly. However, in Main(), there is a call to a method from another assembly and it doesn't get referenced in the standard w...

How do I let Reflection.Emit assemblies access internal members in the generating assembly?

For one of my projects, I need to generate at run time some classes, and I thought it would be fairly simple to do using Reflection.Emit, but I'm getting MemberAccessExceptions when I run some of the generated code that calls methods that are marked internal in the generator assembly. Is there any way to tell the runtime that the dynami...

How do I get the mdTypeRef for a System.Byte for use with IMetaDataEmit?

I just getting started using IMetaDataEmit to write some .NET IL. I'm having trouble getting the mdTypeRef for a System.Byte (which I would like to use to declare a byte array on the stack). Here is my attempt. metaDataEmit->DefineTypeRefByName(NULL, L"System.Byte", &byteToken); This throws an error message that says "Could not load...

Destroy the EnumBuilder After Creating an Enum?

I am trying to reduce the amount of memory my application uses. In my application, I use System.Reflection.Emit.EnumBuilder to create enumerations on the fly from information I receive at runtime. I only use the EnumBuilder to create an individual enumeration, and after that I have no further use for it. While using CLRProfiler, I not...

Reflection.Emit - access topmost-but-one item from stack

Is there a way in .NET, using Reflection.Emit, to access the topmost-but-one item from the stack? So if A is topmost, and B next - I want to process B then A. It would be fine to duplicate B above A (since I can simply "pop" the second B when I get to it). Currently, I am declaring a local: LocalBuilder loc = il.DeclareLocal(typeof...

How does the .NET runtime determine that two types are the same?

Hello, I have assembly A that depends (statically) on type T (reference type, a class) in assembly B. I do not own assembly A but I do own assembly B. T unfortunately is a real type (not an interface) but luckily A uses reflection to discover its members. I want to be able to create dynamically B (and T). The only important item is th...

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

Hello, 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 get the types Then at the end of compilation it emits the resolved types with Re...

Creating a typefrom Xml

Hi, i have a xml like this I want to parse the xml, build a dynamic class with spscified properties. i found some pointers to do it with system.reflection.emit namespace, but i do i always have to create an assembly and module in order to define the type? can i just create a type and define the properties? <Root> <type> <name>mytype<...

Nested enums with System.Reflection.Emit

I want to create a class with a nested enum. public class Foo { public enum Views { } } However System.Reflection.Emit.TypeBuilder class has no DefineNestedEnum only DefinedNestedType. ModuleBuilder.DefineEnum exists that let's me create an enum but I find no way to make it nested. Can I create an enum without faking it ...

What pitfalls to expect from generation of classes from database tables using Reflection.Emit and xsd files ?!

I am playing with class generation ( one class for a table - inheritance etc. not to be considered for now ... ). So I copied shamelessly from here the Reflection.Emit code. Reworked it to be generated per table in a given database and created the files with the following batch call in the Project's bin folder : for /f "tokens=*" %%...

Recommend a book on Reflection.Emit in C#

Can anyone recommend a good book which covers IL programming, specifically using Reflection.Emit in C#? Thanks! ...