Compartilhar via


Como determinar se um arquivo é um assembly (C# e Visual Basic)

Um arquivo é um assembly, se e somente se ele é gerenciado e contém uma entrada de assembly nos metadados. Para obter mais informações sobre metadados e assemblies, consulte o tópico Manifesto de um assembly.

Como determinar manualmente se um arquivo é um assembly

  1. Iniciar o Ildasm.exe (IL Disassembler).

  2. Carrega o arquivo que você deseja testar.

  3. Se ILDASM relatórios que o arquivo não é um arquivo executável portátil (PE), não é um assembly. Para obter mais informações, consulte Como exibir o conteúdo de um assembly.

Como determinar programaticamente se um arquivo é um assembly

  1. Chamar o GetAssemblyName método, passando o caminho completo do arquivo e o nome do arquivo que você está testando.

  2. Se um BadImageFormatException exceção é lançada, o arquivo não é um assembly.

Exemplo

Este exemplo testa uma DLL para ver se ele é um assembly.

Module Module1
    Sub Main()
        Try 
            Dim testAssembly As Reflection.AssemblyName =
                                Reflection.AssemblyName.GetAssemblyName("C:\Windows\Microsoft.NET\Framework\v3.5\System.Net.dll")
            Console.WriteLine("Yes, the file is an Assembly.")
        Catch ex As System.IO.FileNotFoundException
            Console.WriteLine("The file cannot be found.")
        Catch ex As System.BadImageFormatException
            Console.WriteLine("The file is not an Assembly.")
        Catch ex As System.IO.FileLoadException
            Console.WriteLine("The Assembly has already been loaded.")
        End Try
        Console.ReadLine()
    End Sub 
End Module 
' Output (with .NET Framework 3.5 installed): 
'        Yes, the file is an Assembly.
class TestAssembly
{
    static void Main()
    {

        try
        {
            System.Reflection.AssemblyName testAssembly =
                System.Reflection.AssemblyName.GetAssemblyName(@"C:\Windows\Microsoft.NET\Framework\v3.5\System.Net.dll");

            System.Console.WriteLine("Yes, the file is an assembly.");
        }

        catch (System.IO.FileNotFoundException)
        {
            System.Console.WriteLine("The file cannot be found.");
        }

        catch (System.BadImageFormatException)
        {
            System.Console.WriteLine("The file is not an assembly.");
        }

        catch (System.IO.FileLoadException)
        {
            System.Console.WriteLine("The assembly has already been loaded.");
        }
    }
}
/* Output (with .NET Framework 3.5 installed):
    Yes, the file is an assembly.
*/

O GetAssemblyName método carrega o arquivo de teste e depois o libera depois que a informação é lida.

Consulte também

Referência

AssemblyName

Conceitos

Guia de Programação em C#

Assemblies e o cache de assemblies global (C# e Visual Basic)

Outros recursos

Guia de programação do Visual Basic