다음을 통해 공유


Importing a Type Library as an Assembly 

COM type definitions usually reside in a type library. In contrast, CLS-compliant compilers produce type metadata in an assembly. The two sources of type information are quite different.

NoteNote

When available, always use the primary interop assembly published by the author of the COM component you intend to incorporate in your managed code. Types in the primary interop assembly have been imported for you and are ready to activate and call from managed code. For details on producing and using primary interop assemblies, see Primary Interop Assemblies.

This topic describes techniques for generating metadata from a type library. The resulting assembly is called an interop assembly.

Generating Metadata

COM type libraries can be stand-alone TLB files, such as Loanlib.tlb. Some type libraries are embedded in the resource section of a DLL or EXE file. Other sources of type library information are OLB and OCX files.

After locating the type library containing the implementation of your target COM type, you have the following options for generating an interop assembly containing type metadata:

  • Visual Studio 2005

    Visual Studio 2005 automatically converts COM types in a type library to metadata in an assembly. For instructions, see How to: Add References to Type Libraries.

  • Type Library Importer (Tlbimp.exe)

    The Type Library Importer provides command-line options to adjust metadata in the resulting interop file, imports types from an existing type library, and generates an interop assembly and a namespace. For instructions, see How to: Generate Interop Assemblies from Type Libraries.

  • TypeLibConverter class

    The TypeLibConverter class (in the System.Runtime.InteropServices namespace) provides methods to convert coclasses and interfaces in a type library to metadata within an assembly. This API produces the same metadata output as Tlbimp.exe. However, unlike Tlbimp.exe, the TypeLibConverter class can convert an in-memory type library to metadata.

  • Custom Wrappers

    When a type library is unavailable or incorrect, one option is to create a duplicate definition of the class or interface in managed source code. You then compile the source code with a compiler that targets the runtime to produce metadata in an assembly.

    To define COM types manually, you must have access to the following items:

    • Precise descriptions of the coclasses and interfaces being defined.

    • A compiler, such as the C# compiler, that can generate the appropriate .NET Framework class definitions.

    • Knowledge of the type library-to-assembly conversion rules.

    Writing a custom wrapper is an advanced technique that you seldom perform. For additional information on generating a custom wrapper, see Customizing Standard Wrappers.

For details about the COM interop import process, see Type Library to Assembly Conversion Summary.

See Also

Tasks

How to: Add References to Type Libraries
How to: Generate Interop Assemblies from Type Libraries

Reference

Type Library Importer (Tlbimp.exe)
TypeLibConverter Class

Concepts

Exposing COM Components to the .NET Framework
Customizing Standard Wrappers
Using COM Types in Managed Code
Compiling an Interop Project
Deploying an Interop Application

Other Resources

Type Library to Assembly Conversion Summary