Share via


Como: Determinar se um arquivo é um Assembly (C# e Visual Basic)

Um arquivo é um assembly apenas 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 do Assembly.

Como determinar manualmente se um arquivo é um assembly

  1. Iniciar o Ildasm. exe (desmontador MSIL).

  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 do 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 é 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 libera-lo depois que a informação é lida.

Consulte também

Referência

AssemblyName

Conceitos

Guia de Programação C#

Assemblies e o Cache de Assembly Global (C# e Visual Basic)

Outros recursos

Guia de programação Visual Basic