Condividi tramite


Procedura: Determinare se un file è un assembly

Un file è un assembly se e solo se è gestito e contiene una voce di assembly nei relativi metadati. Per altre informazioni su assembly e metadati, vedere Manifesto dell'assembly.

Come determinare manualmente se un file è un assembly

  1. Avviare lo strumento Ildasm.exe (Disassembler IL).

  2. Caricare il file da testare.

  3. Se ILDASM segnala che il file non è un file eseguibile portabile (PE), non è un assembly. Per altre informazioni, vedere l'argomento Procedura: Visualizzare il contenuto dell'assembly.

Come determinare a livello di codice se un file è un assembly

Uso della classe AssemblyName

  1. Chiamare il AssemblyName.GetAssemblyName metodo , passando il percorso completo del file e il nome del file di cui si sta eseguendo il test.

  2. Se viene generata un'eccezione BadImageFormatException , il file non è un assembly.

In questo esempio viene verificata una DLL per verificare se si tratta di un 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.  

Il GetAssemblyName metodo carica il file di test e quindi lo rilascia dopo la lettura delle informazioni.

Uso della classe PEReader

Attenzione

PEReader e la System.Reflection.Metadata libreria non sono progettate per gestire l'input non attendibile. I file PE in formato non valido o dannosi possono causare comportamenti imprevisti, tra cui l'accesso alla memoria fuori dai limiti, crash di sistema o blocchi. Usare queste API solo con assembly attendibili.

  1. Se si sta utilizzando .NET Standard o .NET Framework, installare il pacchetto NuGet System.Reflection.Metadata. Quando la destinazione è .NET Core o .NET 5+, questo passaggio non è necessario perché questa libreria è inclusa nel framework condiviso.

  2. Creare un'istanza System.IO.FileStream per leggere i dati dal file di cui si sta testando.

  3. Crea un'istanza di System.Reflection.PortableExecutable.PEReader, passando il flusso di file nel costruttore.

  4. Controllare il valore della HasMetadata proprietà . Se il valore è false, il file non è un assembly.

  5. Chiamare il GetMetadataReader metodo nell'istanza del lettore PE per creare un lettore di metadati.

  6. Controllare il valore della IsAssembly proprietà . Se il valore è true, il file è un assembly.

A differenza del GetAssemblyName metodo , la PEReader classe non genera un'eccezione nei file PE (Portable Executable) nativi. Ciò consente di evitare il costo aggiuntivo delle prestazioni causato da eccezioni quando è necessario controllare tali file. È comunque necessario gestire le eccezioni nel caso in cui il file non esista o non sia un file PE.

In questo esempio viene illustrato come determinare se un file è un assembly che usa la 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.

Vedere anche