Compartilhar via


Método TypeLibConverter.ConvertTypeLibToAssembly (Object, String, TypeLibImporterFlags, ITypeLibImporterNotifySink, Byte[], StrongNameKeyPair, String, Version)

 

Dica

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Converte uma biblioteca de tipo COM em um assembly.

Namespace:   System.Runtime.InteropServices
Assembly:  mscorlib (em mscorlib.dll)

Sintaxe

[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public AssemblyBuilder ConvertTypeLibToAssembly(
    object typeLib,
    string asmFileName,
    TypeLibImporterFlags flags,
    ITypeLibImporterNotifySink notifySink,
    byte[] publicKey,
    StrongNameKeyPair keyPair,
    string asmNamespace,
    Version asmVersion
)
public:
[SecurityPermissionAttribute(SecurityAction::Demand, Flags = SecurityPermissionFlag::UnmanagedCode)]
virtual AssemblyBuilder^ ConvertTypeLibToAssembly(
    Object^ typeLib,
    String^ asmFileName,
    TypeLibImporterFlags flags,
    ITypeLibImporterNotifySink^ notifySink,
    array<unsigned char>^ publicKey,
    StrongNameKeyPair^ keyPair,
    String^ asmNamespace,
    Version^ asmVersion
) sealed
[<SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)>]
abstract ConvertTypeLibToAssembly : 
        typeLib:Object *
        asmFileName:string *
        flags:TypeLibImporterFlags *
        notifySink:ITypeLibImporterNotifySink *
        publicKey:byte[] *
        keyPair:StrongNameKeyPair *
        asmNamespace:string *
        asmVersion:Version -> AssemblyBuilder
[<SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)>]
override ConvertTypeLibToAssembly : 
        typeLib:Object *
        asmFileName:string *
        flags:TypeLibImporterFlags *
        notifySink:ITypeLibImporterNotifySink *
        publicKey:byte[] *
        keyPair:StrongNameKeyPair *
        asmNamespace:string *
        asmVersion:Version -> AssemblyBuilder
<SecurityPermissionAttribute(SecurityAction.Demand, Flags := SecurityPermissionFlag.UnmanagedCode)>
Public Function ConvertTypeLibToAssembly (
    typeLib As Object,
    asmFileName As String,
    flags As TypeLibImporterFlags,
    notifySink As ITypeLibImporterNotifySink,
    publicKey As Byte(),
    keyPair As StrongNameKeyPair,
    asmNamespace As String,
    asmVersion As Version
) As AssemblyBuilder

Parâmetros

  • typeLib
    Type: System.Object

    O objeto que implementa a interface do ITypeLib.

  • asmFileName
    Type: System.String

    O nome do arquivo do assembly resultante.

  • publicKey
    Type: System.Byte[]

    Uma matriz byte que contém a chave pública.

  • asmNamespace
    Type: System.String

    O namespace do assembly resultante.

  • asmVersion
    Type: System.Version

    A versão do assembly resultante. Se null, a versão da biblioteca de tipos será usada.

Valor Retornado

Type: System.Reflection.Emit.AssemblyBuilder

Um objeto AssemblyBuilder que contém a biblioteca de tipos convertida.

Implementa

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

Exceções

Exception Condition
ArgumentNullException

typeLib é null.

-ou-

asmFileName é null.

-ou-

notifySink é null.

ArgumentException

asmFileName é uma cadeia de caracteres vazia.

-ou-

asmFileName é maior que MAX_PATH.

InvalidOperationException

flags não é PrimaryInteropAssembly.

-ou-

publicKey e keyPair são null.

ReflectionTypeLoadException

Os metadados produzido têm erros que impedem o carregamento de qualquer tipo.

Comentários

Se você não quiser gerar um nome forte para o assembly, é válido para publicKey e keyPair ser null, contanto que flags não é igual a TypeLibImporterFlags.PrimaryInteropAssembly. Caso contrário, pelo menos um desses parâmetros deve ser especificado. Se publicKey é null, a chave pública no keyPair será definido em metadados do manifesto do assembly de destino e uma assinatura será gerada com base no conteúdo do assembly. Se keyPair é null, publicKey será definido em metadados do manifesto do assembly de destino e nenhuma assinatura será gerada. Especificar os dois parâmetros não é geralmente útil e pode resultar em uma assinatura inválida.

Para obter mais informações sobre ITypeLib, consulte sua documentação existente na biblioteca MSDN.

Exemplos

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; 
    }   
}
Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Runtime.InteropServices

Public Class App
    Private Enum RegKind
        RegKind_Default = 0
        RegKind_Register = 1
        RegKind_None = 2
    End Enum 'RegKind

    <DllImport("oleaut32.dll", CharSet:=CharSet.Unicode, PreserveSig:=False)> _
    Private Shared Sub LoadTypeLibEx(ByVal strTypeLibName As [String], ByVal regKind As RegKind, <MarshalAs(UnmanagedType.Interface)> ByRef typeLib As [Object])
    End Sub

    Public Shared Sub Main()
        Dim typeLib As [Object]
        LoadTypeLibEx("SHDocVw.dll", RegKind.RegKind_None, typeLib)

        If typeLib Is Nothing Then
            Console.WriteLine("LoadTypeLibEx failed.")
            Return
        End If

        Dim converter As New TypeLibConverter()
        Dim eventHandler As New ConversionEventHandler()
        Dim asm As AssemblyBuilder = converter.ConvertTypeLibToAssembly(typeLib, "ExplorerLib.dll", 0, eventHandler, Nothing, Nothing, Nothing, Nothing)
        asm.Save("ExplorerLib.dll")
    End Sub 'Main
End Class 'App
 _

Public Class ConversionEventHandler
    Implements ITypeLibImporterNotifySink

    Public Sub ReportEvent(ByVal eventKind As ImporterEventKind, ByVal eventCode As Integer, ByVal eventMsg As String) Implements ITypeLibImporterNotifySink.ReportEvent
        ' handle warning event here...
    End Sub 'ReportEvent

    Public Function ResolveRef(ByVal typeLib As Object) As [Assembly] Implements ITypeLibImporterNotifySink.ResolveRef
        ' resolve reference here and return a correct assembly...
        Return Nothing
    End Function 'ResolveRef
End Class 'ConversionEventHandler
using namespace System;
using namespace System::Reflection;
using namespace System::Reflection::Emit;
using namespace System::Runtime::InteropServices;

enum class RegKind
{
   RegKind_Default, RegKind_Register, RegKind_None
};

ref class ConversionEventHandler: public ITypeLibImporterNotifySink
{
public:
   virtual void ReportEvent( ImporterEventKind eventKind, int eventCode, String^ eventMsg )
   {

      // handle warning event here...
   }

   virtual Assembly^ ResolveRef( Object^ typeLib )
   {

      // resolve reference here and return a correct assembly...
      return nullptr;
   }

};


[DllImport("oleaut32.dll",CharSet=CharSet::Unicode,PreserveSig=false)]
extern void LoadTypeLibEx( String^ strTypeLibName, RegKind regkind,
         [MarshalAs(UnmanagedType::Interface)] interior_ptr<Object^> typeLib );

int main()
{
   Object^ typeLib = gcnew Object;
   LoadTypeLibEx( "SHDocVw.dll", RegKind::RegKind_None,  &typeLib );
   if ( typeLib == nullptr )
   {
      Console::WriteLine( "LoadTypeLibEx failed." );
      return 0;
   }

   TypeLibConverter^ converter = gcnew TypeLibConverter;
   ConversionEventHandler^ eventHandler = gcnew ConversionEventHandler;
   AssemblyBuilder^ asmb = converter->ConvertTypeLibToAssembly( typeLib, "ExplorerLib.dll", (System::Runtime::InteropServices::TypeLibImporterFlags)0, eventHandler, nullptr, nullptr, nullptr, nullptr );
   asmb->Save( "ExplorerLib.dll" );
}

Segurança

SecurityPermission

for permission to call unmanaged code.

Security Action: Demand.

Associated Enumeration: F:System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode

Informações de Versão

.NET Framework
Disponível desde 1.1

Confira Também

ConvertTypeLibToAssembly Sobrecarga
Classe TypeLibConverter
Namespace System.Runtime.InteropServices
Atributos para importar bibliotecas de tipos em assemblies de interoperabilidade

Retornar ao início