类型等效性和嵌入的互操作类型

     

         从 .NET Framework 4 版开始,公共语言运行时支持将 COM 类型的类型信息直接嵌入到托管程序集中,而不是要求托管程序集从互操作程序集中获取 COM 类型的类型信息。 由于嵌入的类型信息只包含托管程序集实际所使用的类型和成员,因此两个托管程序集可能会具有相同 COM 类型的截然不同的视图。 每个托管程序集使用不同的 Type 对象来表示各自的 COM 类型视图。 公共语言运行时支持这些不同视图之间的类型等效性,这些类型包括接口、结构、枚举和委托。

类型等效性意味着,在两个托管程序集之间传递的 COM 对象在接收程序集中可以转换为适当的托管类型。

注意注意

类型等效性和嵌入的互操作类型可简化使用 COM 组件的应用程序和外接程序的部署过程,这是因为不需要使用应用程序来部署互操作程序集。 共享 COM 组件的开发人员若想使其组件可供早期版本的 .NET Framework 使用,则他们仍然必须创建主互操作程序集 (PIA)。

 
 
类型等效性

COM 类型等效性支持的类型包括接口、结构、枚举和委托。 如果满足下面的所有条件,则 COM 类型符合等效要求:

  • 两个类型同时为接口、结构、枚举或委托。

  • 两个类型具有相同的标识,如下一节中所述。

  • 两个类型符合类型等效性要求,如针对类型等效性标记 COM 类型一节中所述。

类型标示

当两个类型的范围和标识相匹配时,确定这两个类型具有相同的标识;也就是说,如果两个类型中的每个类型都具有 TypeIdentifierAttribute 特性,则这两个特性具有匹配的 Scope 和 Identifier 属性。 Scope 的比较不区分大小写。

如果某个类型不具有 TypeIdentifierAttribute 特性,或者具有未指定范围和标识符的 TypeIdentifierAttribute 特性,则仍可以将该类型视为符合等效性,如下所示:

  • GuidAttribute is used instead of the TypeIdentifierAttribute.Scope property, and the Type.FullName property (that is, the type name, including the namespace) is used instead of the TypeIdentifierAttribute.Identifier property." style="margin: 0px; padding: 0px; border: 0px; outline: 0px; line-height: 16px; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">对于接口,使用 GuidAttribute 的值来代替使用 TypeIdentifierAttribute.Scope 属性,并使用 Type.FullName 属性(即,包括命名空间的类型名称)来代替使用 TypeIdentifierAttribute.Identifier 属性。

  • GuidAttribute of the containing assembly is used instead of the Scope property, and the Type.FullName property is used instead of the Identifier property." style="margin: 0px; padding: 0px; border: 0px; outline: 0px; line-height: 16px; background-color: transparent; background-position: initial initial; background-repeat: initial initial;">对于结构、枚举和委托,使用包含程序集的 GuidAttribute 来代替使用 Scope 属性,并使用 Type.FullName 属性来代替使用 Identifier 属性。

针对类型等效性标记 COM 类型

可以通过以下两种方式将一个类型标记为符合类型等效性:

原文地址:https://www.cnblogs.com/tianma3798/p/3499337.html