TypeLibConverter.ConvertTypeLibToAssembly Method

Definition

Converts a COM type library to an assembly.

Overloads

ConvertTypeLibToAssembly(Object, String, Int32, ITypeLibImporterNotifySink, Byte[], StrongNameKeyPair, Boolean)

Converts a COM type library to an assembly.

public System.Reflection.Emit.AssemblyBuilder ConvertTypeLibToAssembly(object typeLib, string asmFileName, int flags, System.Runtime.InteropServices.ITypeLibImporterNotifySink notifySink, byte[] publicKey, System.Reflection.StrongNameKeyPair keyPair, bool unsafeInterfaces);

Parameters

typeLib
Object

The object that implements the ITypeLib interface.

asmFileName
String

The file name of the resulting assembly.

flags
Int32

A TypeLibImporterFlags value indicating any special settings.

notifySink
ITypeLibImporterNotifySink

ITypeLibImporterNotifySink interface implemented by the caller.

publicKey
Byte[]

A byte array containing the public key.

keyPair
StrongNameKeyPair

A StrongNameKeyPair object containing the public and private cryptographic key pair.

unsafeInterfaces
Boolean

If true, the interfaces require link time checks for UnmanagedCode permission. If false, the interfaces require run time checks that require a stack walk and are more expensive, but help provide greater protection.

Returns

An AssemblyBuilder object containing the converted type library.

Implements

Exceptions

typeLib is null.

-or-

asmFileName is null.

-or-

notifySink is null.

asmFileName is an empty string.

-or-

asmFileName is longer than the system-defined maximum length. For more information, see PathTooLongException.

flags is not PrimaryInteropAssembly.

-or-

publicKey and keyPair are null.

The metadata produced has errors preventing any types from loading.

Remarks

If you do not want to generate a strong name for your assembly, it is valid for publicKey and keyPair to be null, as long as flags does not equal TypeLibImporterFlags.PrimaryInteropAssembly. Otherwise, at least one of these parameters must be specified. If publicKey is null, the public key in keyPair will be set in the target assembly's manifest metadata and a signature will be generated based on the contents of the assembly. If keyPair is null, publicKey will be set in the target assembly's manifest metadata and no signature will be generated. Specifying both parameters is not generally useful, and can result in an invalid signature.

For more information on ITypeLib, please see its existing documentation in the MSDN library.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

ConvertTypeLibToAssembly(Object, String, TypeLibImporterFlags, ITypeLibImporterNotifySink, Byte[], StrongNameKeyPair, String, Version)

Converts a COM type library to an assembly.

public System.Reflection.Emit.AssemblyBuilder ConvertTypeLibToAssembly(object typeLib, string asmFileName, System.Runtime.InteropServices.TypeLibImporterFlags flags, System.Runtime.InteropServices.ITypeLibImporterNotifySink notifySink, byte[] publicKey, System.Reflection.StrongNameKeyPair keyPair, string asmNamespace, Version asmVersion);

Parameters

typeLib
Object

The object that implements the ITypeLib interface.

asmFileName
String

The file name of the resulting assembly.

flags
TypeLibImporterFlags

A TypeLibImporterFlags value indicating any special settings.

notifySink
ITypeLibImporterNotifySink

ITypeLibImporterNotifySink interface implemented by the caller.

publicKey
Byte[]

A byte array containing the public key.

keyPair
StrongNameKeyPair

A StrongNameKeyPair object containing the public and private cryptographic key pair.

asmNamespace
String

The namespace for the resulting assembly.

asmVersion
Version

The version of the resulting assembly. If null, the version of the type library is used.

Returns

An AssemblyBuilder object containing the converted type library.

Implements

Exceptions

typeLib is null.

-or-

asmFileName is null.

-or-

notifySink is null.

asmFileName is an empty string.

-or-

asmFileName is longer than the system-defined maximum length. For more information, see PathTooLongException.

flags is not PrimaryInteropAssembly.

-or-

publicKey and keyPair are null.

The metadata produced has errors preventing any types from loading.

Examples

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;

public class App
{
    private enum RegKind
    {
        RegKind_Default = 0,
        RegKind_Register = 1,
        RegKind_None = 2
    }
    
    [ DllImport( "oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = false )]
    private static extern void LoadTypeLibEx( String strTypeLibName, RegKind regKind, 
        [ MarshalAs( UnmanagedType.Interface )] out Object typeLib );
    
    public static void Main()
    {
        Object typeLib;
        LoadTypeLibEx( "SHDocVw.dll", RegKind.RegKind_None, out typeLib ); 
        
        if( typeLib == null )
        {
            Console.WriteLine( "LoadTypeLibEx failed." );
            return;
        }
            
        TypeLibConverter converter = new TypeLibConverter();
        ConversionEventHandler eventHandler = new ConversionEventHandler();
        AssemblyBuilder asm = converter.ConvertTypeLibToAssembly( typeLib, "ExplorerLib.dll", 0, eventHandler, null, null, null, null );	
        asm.Save( "ExplorerLib.dll" );
    }
}

public class ConversionEventHandler : ITypeLibImporterNotifySink
{
    public void ReportEvent( ImporterEventKind eventKind, int eventCode, string eventMsg )
    {
        // handle warning event here...
    }
    
    public Assembly ResolveRef( object typeLib )
    {
        // resolve reference here and return a correct assembly...
        return null; 
    }	
}

Remarks

If you do not want to generate a strong name for your assembly, it is valid for publicKey and keyPair to be null, as long as flags does not equal TypeLibImporterFlags.PrimaryInteropAssembly. Otherwise, atleast one of these parameters must be specified. If publicKey is null, the public key in keyPair will be set in the target assembly's manifest metadata and a signature will be generated based on the contents of the assembly. If keyPair is null, publicKey will be set in the target assembly's manifest metadata and no signature will be generated. Specifying both parameters is not generally useful, and can result in an invalid signature.

For more information on ITypeLib, please see its existing documentation in the MSDN library.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1