Dynamically creating an assembly targeting a specific .NET runtime using Reflection.Emit
Asked Answered
A

1

11

I'm using Reflection.Emit to develop a tool that dynamically creates an Assembly at runtime.

The tool is targeting the .NET 4.5 framework.

I'd like to know if it's possible to specify which .NET runtime the dynamically generated assembly targets (e.g: specify that a .NET 3.5 assembly will be created, for example).

Anglocatholic answered 16/12, 2013 at 12:14 Comment(0)
D
6

The inbuilt reflection-emit is pretty limited here; what you want to do is tell it to use a specific mscorlib assembly, but the problem is that a lot of reflection-emit involves passing Types around, which makes this incredibly hard. The most pragmatic way I found to approach this problem was to switch to IKVM.Reflection.dll - part of IKVM.NET. This dll has very deliberately the same basic API as Reflection.Emit, but instead of operating against the inbuilt Type objects, it operates against the IKVM instances, which are loaded in the concept of a Universe. A Universe can then load the desired mscorlib dll, and whichever other dlls you require.

The changes for this is usually just changing the using statements. This approach is used throughout protobuf-net (in particular the precompile tool), allowing not just different versions, but entire different frameworks to be targeted. Want to create a dll that targets silverlight from a regular .NET application? Not a problem. The trickiest bit (IMO) becomes simply finding the correct mscorlib and supporting files to load into the Universe.

See my blog post Enter the IKVM - or see the examples on IKVM, like Function Pointer Types.

I can provide more info as required.

Donoho answered 16/12, 2013 at 12:27 Comment(9)
Thanks. Isn't it possible to "fool" the Type instances to make them load from the desired mscorlib?Anglocatholic
@Anglocatholic if it is, I never managed to get it to work despite lots of trying. IKVM, however, worked pretty much first time.Donoho
@Anglocatholic as a side note, it may well be that when Roslyn finally ships, there are pieces in there for making it work... who knowsDonoho
How would Roslyn help? i thought it operates at the source code levelAnglocatholic
I wonder whether code that uses C++/CLI function pointers (which is a feature that you pretty much won't need in normal .Net code) is actually a good example.Cogitative
@Cogitative the important thing is only it showing how to create an Universe; all the usual methods that exist in AssemblyBuilder are available from thereDonoho
@Anglocatholic maybe; perhaps I'm being too hopefulDonoho
Another thing i considered was using a Reflection Only context, resolving to the required mscorlib at runtime. not sure if that's possible.Anglocatholic
@Anglocatholic You can use Roslyn to generate an assembly at runtime, so it's not inconcievable that you could use it to generate assembly that targets different version of the framework. The fact that it generates assemblies from C# and not IL doesn't actually matter.Cogitative

© 2022 - 2024 — McMap. All rights reserved.