类型等效性和嵌入的互操作类型Type equivalence and embedded interop types

从 .NET Framework 4 开始,公共语言运行时支持将 COM 类型的类型信息直接嵌入到托管程序集中,而不要求托管程序集从互操作程序集中获取 COM 类型的类型信息。Beginning with the .NET Framework 4, the common language runtime supports embedding type information for COM types directly into managed assemblies, instead of requiring the managed assemblies to obtain type information for COM types from interop assemblies. 由于嵌入式类型信息仅包含托管程序集实际使用的类型和成员,因此两个托管程序集可能具有相同 COM 类型的不同视图。Because the embedded type information includes only the types and members that are actually used by a managed assembly, two managed assemblies might have very different views of the same COM type. 每个托管程序集都有不同的 Type 对象来表示其 COM 类型视图。Each managed assembly has a different Type object to represent its view of the COM type. 公共语言运行时支持接口、结构、枚举和委托等不同视图之间的类型等效性。The common language runtime supports type equivalence between these different views for interfaces, structures, enumerations, and delegates.

类型等效性意味着从一个托管程序集传递到另一个托管程序集的 COM 对象可以转换为接收程序集中适当的托管类型。Type equivalence means that a COM object that is passed from one managed assembly to another can be cast to the appropriate managed type in the receiving assembly.

备注

类型等效性和嵌入式互操作类型简化了使用 COM 组件的应用程序和加载项的部署,因为无需与应用程序一起部署互操作程序集。Type equivalence and embedded interop types simplify the deployment of applications and add-ins that use COM components, because it is not necessary to deploy interop assemblies with the applications. 如果共享 COM 组件的开发人员希望较早版本的 .NET Framework 使用其组件,他们仍须创建主互操作程序集 (PIA)。Developers of shared COM components still have to create primary interop assemblies (PIAs) if they want their components to be used by earlier versions of the .NET Framework.

类型等效性Type equivalence

COM 类型的等效性支持接口、结构、枚举和委托。Equivalence of COM types is supported for interfaces, structures, enumerations, and delegates. 如果满足以下所有条件,则 COM 类型符合等效条件:COM types qualify as equivalent if all of the following are true:

  • 类型是两个接口、两个结构、两个枚举或两个委托。The types are both interfaces, or both structures, or both enumerations, or both delegates.

  • 类型具有相同标识,如下节所述。The types have the same identity, as described in the next section.

  • 两种类型都符合类型等效性,如针对类型等效性标记 COM 类型部分所述。Both types are eligible for type equivalence, as described in the Marking COM types for type equivalence section.

类型标识Type identity

范围和标识匹配时,确定两种类型具有相同标识,换句话说,如果它们各自具有 TypeIdentifierAttribute 属性,并且两个属性都具有匹配的 ScopeIdentifier 属性。Two types are determined to have the same identity when their scopes and identities match, in other words, if they each have the TypeIdentifierAttribute attribute, and the two attributes have matching Scope and Identifier properties. Scope 的比较不区分大小写。The comparison for Scope is case-insensitive.

如果一个类型不具有 TypeIdentifierAttribute 属性,或者如果它有一个不指定范围和标识符的 TypeIdentifierAttribute 属性,仍可将该类型视为等效性,如下所示:If a type does not have the TypeIdentifierAttribute attribute, or if it has a TypeIdentifierAttribute attribute that does not specify scope and identifier, the type can still be considered for equivalence as follows:

针对类型等效性标记 COM 类型Marking COM types for type equivalence

可通过两种方式将类型标记为符合类型等效性:You can mark a type as eligible for type equivalence in two ways:

请参阅See also