Type equivalence and embedded InterOP types

Source: Internet
Author: User

Beginning with. net Framework Version 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. because the embedded type information provided des 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. 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.

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 processing assembly.

Note

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. 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. net Framework.

Type equivalence

Equivalence of COM types is supported for interfaces, structures, enumerations, and delegates. 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.

    • Both types are eligible for Type equivalence, as described in the marking COM types for Type equivalence section.

Type identity

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. the comparison for scope is case-insensitive.

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:

    • For interfaces, the value of the guidattries 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.

    • For structures, enumerations, and delegates, the guidattriures of the containing assembly is used instead of the scope property, and the type.Fullnameproperty is used instead of the identifier property.

Marking COM types for Type equivalence

You can mark a type as eligible for Type equivalence in two ways:

    • Apply the typeidentifierattribute to the type.

    • Make the type a com import type. an interface is a com import type if it has the comimportattribute. an interface, structure, enumeration, or delegate is a com import type if the assembly in which it is defined has the importedfromtypelibattribute.

Using COM type in managed code

COM types that are defined in an assembly are like any other managed types. managed clients can create a new instance of a COM type in the usual way and obtain class information through metadata as they wocould for any other Managed class. method syntax can be inspected through an object viewer or obtained using reflection, just as it can with any other Managed class. when the COM Object returns a failure hresult,. net Framework client catches a corresponding exception.

there are two ways for your application to include the type information that enables interoperation with COM types:

    • using embedded InterOP types: Beginning with. net Framework Version 4, you can instruct the compiler to embed type information from an InterOP assembly into your executable. the compiler embeds only the type information that your application uses. you do not have to deploy the InterOP assembly with your application. this is the recommended technique.

    • deploying InterOP assemblies: You can create a standard reference to an InterOP assembly. in this case, the InterOP assembly must be deployed with your application. if you employ this technique, and you are not using a private COM component, always reference the primary InterOP assembly (PIA) published by the author of the COM component you intend to in1_ate in your managed code. for more information about producing and using primary InterOP assemblies, see primary InterOP assemblies.

Note

When you use embedded InterOP types, you can embed them from the primary InterOP Assembly published by the author of the COM component. however, you do not have to deploy the primary InterOP assembly with your application.

Using embedded InterOP types reduces the size of your application, because most applications do not use all the features of a COM component. the compiler is very efficient when it embeds type information; if your application uses only some of the methods on a COM interface, the compiler does not embed the unused methods. when an application that has embedded type information interacts with another such application, or interacts with an application that uses a primary InterOP assembly, the Common Language Runtime uses type equivalence rules to determine whether two types with the same name represent the same com type.

Obtaining and releasing a reference to a running COM object is just like obtaining and releasing a reference to any other running managed object. when. net Framework clients obtain and release a reference to a COM object, the runtime maintains the reference count on the COM object just as any other COM Client wocould, and. net Framework clients can behave as if the object were subject to garbage collection, just as they wocould for any other managed Server Object.

Visual Studio makes it easy to embed type information into an application or add-in. for examples, see Walkthrough: embedding type information from Microsoft Office assemblies (C # and Visual Basic) and Walkthrough: embedding types from managed assemblies (C # and Visual Basic ).

For code samples that accompany the topics of this section, see COM InterOP sample:. Net client and COM server.

The rules for Type equivalence are discussed in type equivalence and embedded InterOP types. However, you do not have to know these rules to use COM objects.

 

 

 

See also

Http://blogs.msdn.com/ B /samng/archive/2010/01/24/the-pain-of-deploying-primary-interop-assemblies.aspx

Test: Check the complied assemble file using reflector, if had set embed InterOP types is true. See whether embed only for the used the type from COM types.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.