.NET Tips and Tricks

Blog archive

Reduce Overhead in COM Interoperability in .NET 4

Probably the only developers who care about this are the .NET developers doing Office development and who are upgrading projects to.NET 4. But if you do work with COM objects from code and are ever planning to move your projects up from .NET 3/3.5, this is worth knowing.

PIAs (Primary Interop Assembly) provide the information that supports interoperability between .NET and COM. PIAs for complex COM objects (think: Microsoft Word) are huge and kept in separate, monolithic assemblies. That means that when you use any member on your COM object, the whole PIA is loaded into memory (not to mention that the PIA was one more thing to be distributed).

Upgrading to .NET 4 can give you some significant benefits through the "NoPIA" feature (also called Type Embedding, but "NoPIA" just sounds cooler). With NoPIA, the necessary information to support COM interoperability is embedded into your compiled .NET code. Not only does that reduce the number of components to be distributed, only the parts of the PIA actually required by your application are incorporated into your code. If you only use a small part of a big COM object you could see significant size reductions in your compiled code.

The option is automatically turned on for new Visual Studio 2010 projects, but if you've imported a project from an earlier version of Visual Studio, you'll need to set it yourself. In your References list, select the reference to your COM object. Then, in the Properties window, find the Embed Interop Types property and set it to True.

Posted by Peter Vogel on 05/24/2011 at 1:16 PM

comments powered by Disqus


Subscribe on YouTube

Upcoming Events