简体   繁体   中英

Can someone explain the benefits of using a Primary Interop Assembly in .Net?

This concept is new to me, and a colleague suggested it. Sadly, I had no idea what he was talking about. Can someone enlighten me?

You can find plenty of information about it here .

In a nutshell, a PIA is a signed interop assembly that provides the "official" definition of types in a COM library from the publisher of the COM library.

As to the benefits, the posted article sums it up pretty good:

PIAs are important because they provide unique type identity. The PIA distinguishes the official type definitions from counterfeit definitions provided by other interop assemblies. Having a single type identity ensures type compatibility between applications that share the types defined in the PIA. Because the PIA is signed by its publisher and labeled with the PrimaryInteropAssembly attribute, it can be differentiated from other interop assemblies that define the same types.

A primary interop assembly will wrap the COM interfaces into .NET compatible types. It doesn't give you the granular control that manually invoking the methods does, but it's close enough.

Without a PIA:

object _comObject;
Type _comObjectType;
_comObjectType = Type.GetTypeFromProgID("MyCompany.MyApplication.MyObject", true);
_comObject = Activator.CreateInstance(_comObjectType);

string name = (string)_comObjectType.InvokeMember("GetCustomerName", BindingFlags.InvokeMethod, null, _comObject, , new object [] { _customerId });

With a PIA:

MyCompany.MyApplication.MyObject obj = new MyObject();
string name = obj.GetCustomerName(_customerId);

简单地通过一个例子来说,如果你想开发一个类似于任何其他具有扩展功能的办公工具(MS word,visio ......)的应用程序,你可以使用PIA在你的项目中使用office工具的功能。在我的类图中绘图应用程序,使用visio面板创建类图。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM