Partilhar via


Como determinar se um ficheiro é um assembly

Um ficheiro é uma assembleia se, e somente se, for gerido, e contiver uma entrada de montagem nos seus metadados. Para mais informações sobre assemblies e metadados, consulte manifesto assembly.

Como determinar manualmente se um ficheiro é um assembly

  1. Inicie a ferramenta Ildasm.exe (IL Disassembler).

  2. Carrega o ficheiro que queres testar.

  3. Se o ILDASM reporta que o ficheiro não é um ficheiro executável portátil (PE), então não é um assembly. Para mais informações, consulte o tópico Como: Ver conteúdos de montagem.

Como determinar programaticamente se um ficheiro é um assembly

Usando a classe AssemblyName

  1. Chama o AssemblyName.GetAssemblyName método, passando o caminho completo do ficheiro e o nome do ficheiro que estás a testar.

  2. Se for lançada uma BadImageFormatException exceção, o ficheiro não é uma assembleia.

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

using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

static class ExampleAssemblyName
{
    public static void CheckAssembly()
    {
        try
        {
            string path = Path.Combine(
                RuntimeEnvironment.GetRuntimeDirectory(),
                "System.Net.dll");

            AssemblyName testAssembly = AssemblyName.GetAssemblyName(path);
            Console.WriteLine("Yes, the file is an assembly.");
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("The file cannot be found.");
        }
        catch (BadImageFormatException)
        {
            Console.WriteLine("The file is not an assembly.");
        }
        catch (FileLoadException)
        {
            Console.WriteLine("The assembly has already been loaded.");
        }
    }

    /* Output: 
    Yes, the file is an assembly.  
    */
}
Imports System
Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices

Module ExampleAssemblyName
    Sub CheckAssembly()
        Try
            Dim filePath As String = Path.Combine(
                RuntimeEnvironment.GetRuntimeDirectory(),
                "System.Net.dll")

            Dim testAssembly As AssemblyName =
                                AssemblyName.GetAssemblyName(filePath)
            Console.WriteLine("Yes, the file is an Assembly.")
        Catch ex As FileNotFoundException
            Console.WriteLine("The file cannot be found.")
        Catch ex As BadImageFormatException
            Console.WriteLine("The file is not an Assembly.")
        Catch ex As FileLoadException
            Console.WriteLine("The Assembly has already been loaded.")
        End Try
    End Sub
End Module
' Output:  
' Yes, the file is an Assembly.  

O GetAssemblyName método carrega o ficheiro de teste e depois liberta-o assim que a informação é lida.

Utilização da classe PEReader

Atenção

PEReader e as System.Reflection.Metadata bibliotecas não foram concebidas para lidar com entradas não confiáveis. Ficheiros PE mal formados ou maliciosos podem causar comportamentos inesperados, incluindo acesso à memória fora dos limites, falhas ou bloqueios. Utilize apenas estas APIs com assemblies de confiança.

  1. Se estiver a direcionar para o .NET Standard ou .NET Framework, instale o pacote System.Reflection.Metadata NuGet. (Ao direcionar .NET Core ou .NET 5+, este passo não é necessário porque esta biblioteca está incluída no framework partilhado.)

  2. Cria uma System.IO.FileStream instância para ler dados do ficheiro que estás a testar.

  3. Crie uma System.Reflection.PortableExecutable.PEReader instância, passando o seu fluxo de ficheiros para o construtor.

  4. Verifique o valor da HasMetadata propriedade. Se o valor for false, o ficheiro não é uma assembleia.

  5. Chame o GetMetadataReader método na instância do leitor PE para criar um leitor de metadados.

  6. Verifique o valor da IsAssembly propriedade. Se o valor for true, o ficheiro é uma assembleia.

Ao contrário do método GetAssemblyName, a classe PEReader não lança uma exceção em ficheiros nativos de Executável Portátil (PE). Isto permite-lhe evitar o custo adicional de desempenho causado por exceções quando precisa de verificar esses ficheiros. Ainda tens de tratar das exceções caso o ficheiro não exista ou não seja um ficheiro PE.

Este exemplo mostra como determinar se um ficheiro é um assembly usando a PEReader classe.

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices;

static class ExamplePeReader
{
    static bool IsAssembly(string path)
    {
        using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

        // Try to read CLI metadata from the PE file.
        using var peReader = new PEReader(fs);

        if (!peReader.HasMetadata)
        {
            return false; // File does not have CLI metadata.
        }

        // Check that file has an assembly manifest.
        MetadataReader reader = peReader.GetMetadataReader();
        return reader.IsAssembly;
    }

    public static void CheckAssembly()
    {
        string path = Path.Combine(
                RuntimeEnvironment.GetRuntimeDirectory(),
                "System.Net.dll");

        try
        {
            if (IsAssembly(path))
            {
                Console.WriteLine("Yes, the file is an assembly.");
            }
            else
            {
                Console.WriteLine("The file is not an assembly.");
            }
        }
        catch (BadImageFormatException)
        {
            Console.WriteLine("The file is not an executable.");
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("The file cannot be found.");
        }
    }

    /* Output: 
    Yes, the file is an assembly.  
    */
}
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Reflection.Metadata
Imports System.Reflection.PortableExecutable
Imports System.Runtime.InteropServices

Module ExamplePeReader
    Function IsAssembly(path As String) As Boolean

        Dim fs As FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

        ' Try to read CLI metadata from the PE file.
        Dim peReader As PEReader = New PEReader(fs)
        Using (peReader)
            If Not peReader.HasMetadata Then
                Return False ' File does Not have CLI metadata.
            End If

            ' Check that file has an assembly manifest.
            Dim reader As MetadataReader = peReader.GetMetadataReader()
            Return reader.IsAssembly
        End Using
    End Function

    Sub CheckAssembly()
        Dim filePath As String = Path.Combine(
                RuntimeEnvironment.GetRuntimeDirectory(),
                "System.Net.dll")

        Try
            If IsAssembly(filePath) Then
                Console.WriteLine("Yes, the file is an assembly.")
            Else
                Console.WriteLine("The file is not an assembly.")
            End If
        Catch ex As BadImageFormatException
            Console.WriteLine("The file is not an executable.")
        Catch ex As FileNotFoundException
            Console.WriteLine("The file cannot be found.")
        End Try
    End Sub
End Module
' Output:  
' Yes, the file is an Assembly.

Consulte também