Procedura: Determinare se un file è un assembly

Un file è un assembly unicamente nei casi in cui è gestito e include nei metadati una voce assembly. Per altre informazioni su assembly e metadati, vedere Manifesto dell'assembly.

Procedura: Determinare se un file è un assembly in modo manuale

  1. Avviare il Disassembler IL (Ildasm.exe).

  2. Caricare il file da testare.

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

Procedura: Determinare se un file è un assembly a livello di codice

Uso della classe AssemblyName

  1. Chiamare il metodo AssemblyName.GetAssemblyName, passando il percorso file completo e il nome del file sottoposto a test.

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

Nell'esempio riportato di seguito viene testata una DLL per verificare se è 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 metodo GetAssemblyName carica il file di test e lo rilascia dopo aver letto le informazioni.

Uso della classe PEReader

  1. Se si usa .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. Creare un'istanza System.Reflection.PortableExecutable.PEReader di , 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.

Vedi anche