Note: This Post transfered from my OLD Blog and was originally posted in 2008.
As new enhancement in CLR version 4.0 (will be released in 2010) is the concept of Type Embedding. The actual motivation for this new concept was the miserable story of deploying application that uses Primary Interop Assemblies (PIA).
So the Scenario was trying to develop against PIAs leads to:
- Complex deployment scenarios
- Targeting multiple hosting environment.
- Tight type coupling.
MS started a new project code-name NOPIA as part of the next version of CLR 4, its target was to eliminate runtime dependency on Interop Assemblies at compile time easily, So you can do that with just flipping a switch in the visual studio assembly reference properties window or just by compiling your code with /link switch.
What’s NOPIA
I’m using Visual Studio 2010, .Net 4.0, and CLR v4.0 CTP to develop this application, you can download this CTP here.
- Create new C# console application.
- Add reference for Microsoft.Office.Interop.Word.dll
- Add this class to your applicaion.
1: using System;
2: using Word = Microsoft.Office.Interop.Word;
3:
4: namespace HelloBuddy
5: {
6: public class Program
7: {
8: static void Main(string[] args)
9: {
10: SayHi("Ahmed");
11: Console.ReadLine();
12: }
13:
14: public static void SayHi(string name)
15: {
16: Word.Application wordApp = new Word.Application();
17:
18: wordApp.Visible = true;
19: wordApp.Activate();
20:
21: object falseValue = false;
22: object trueValue = true;
23: object missing = Type.Missing;
24:
25: Word.Document doc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
26:
27: object start1 = 0;
28: object end1 = 0;
29:
30: Word.Range rng = doc.Range(ref start1, ref missing);
31: rng.Font.Name = "Tahoma";
32: rng.InsertAfter("Hello " + name);
33:
34: }
35:
36: }
37: }
Now let’s switch to NOPIA Mode, and embed those PIA used types into our console assembly, to do that:
If you check the Bin directory you will find only the application assembly ”HelloBuddy.exe ”, there is no PIA assembly. The Only difference is application assembly becoming little bit bigger because it now embed the partial type info from the original PIA, but still smaller than deploying the whole PIA file. Attach “HelloBudy.exe” to your VS Debugger, and check the loaded modules, you will find only your application assembly, no more PIA. Behind the Scene If you are interested to know what actually CLR do behind the scenes, open your application assembly with Reflector and check the types inside it, you will find that CLR has injected Microsoft.Office.interop.Word namespace into your application’s assembly. |
NOPIA Limitations
Ofcourse there is nothing without limitations. NOPIA has some limitation of what you can embed into your assemblies:
- Can’t Embed IL (no classes or static methods)
- Only metadata is locally embedded (interfaces, delegates, enums, structs )
- Only types from Interop Assemblies can be embedded. Compilers check for these attributesa. [assembly:Guid(…)]b. [assembly:ImportedFromTypeLib(…)]
As you can see how the MS Future CLR v4.0 provides an easy way to develop application against COM interop assemblies, along with very friendly, powerful deployment scenario.
Related stuff
- Download sample application source code
- PDC 2008: TL02 Under the Hood – Advances in the .NET Type System (Video | PPTX)
- Check CLR 4.0 advanced related topic “Type Equivalence” on Misha Shneerson’s blog
Hope this helps,
Ahmed