Postupy: Určení, jestli je soubor sestavením

Soubor je sestavení, pokud a pouze pokud je spravováno, a obsahuje položku sestavení v jeho metadatech. Další informace o sestaveních a metadatech naleznete v manifestu sestavení.

Jak ručně určit, jestli je soubor sestavením

  1. Spusťte nástroj Ildasm.exe (IL Disassembler).

  2. Načtěte soubor, který chcete otestovat.

  3. Pokud ILDASM hlásí, že soubor není přenosný spustitelný soubor (PE), nejedná se o sestavení. Další informace naleznete v tématu Postupy: Zobrazení obsahu sestavení.

Jak programově určit, jestli je soubor sestavením

Použití třídy AssemblyName

  1. Zavolejte metodu AssemblyName.GetAssemblyName a předejte úplnou cestu k souboru a název souboru, který testujete.

  2. BadImageFormatException Pokud je vyvolán výjimka, soubor není sestavení.

Tento příklad otestuje knihovnu DLL a zjistí, jestli se jedná o sestavení.

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.  

Metoda GetAssemblyName načte testovací soubor a po přečtení informací ho uvolní.

Použití třídy PEReader

  1. Pokud cílíte na .NET Standard nebo .NET Framework, nainstalujte System.ReflexeIon. Balíček NuGet metadat. (Pokud cílíte na .NET Core nebo .NET 5 nebo novější, tento krok se nevyžaduje, protože tato knihovna je součástí sdílené architektury.)

  2. Vytvořte System.IO.FileStream instanci pro čtení dat ze souboru, který testujete.

  3. Vytvořte System.Reflection.PortableExecutable.PEReader instanci a předejte stream souboru do konstruktoru.

  4. Zkontrolujte hodnotu HasMetadata vlastnosti. Pokud je falsehodnota , soubor není sestavení.

  5. GetMetadataReader Volání metody v instanci čtenáře pe k vytvoření čtečky metadat.

  6. Zkontrolujte hodnotu IsAssembly vlastnosti. Pokud je truehodnota , soubor je sestavení.

GetAssemblyName Na rozdíl od metody PEReader třída nevyvolá výjimku u nativních souborů Portable Executable (PE). Díky tomu se můžete vyhnout dodatečným nákladům na výkon způsobeným výjimkami, když je potřeba tyto soubory zkontrolovat. V případě, že soubor neexistuje nebo není soubor PE, stále potřebujete zpracovat výjimky.

Tento příklad ukazuje, jak určit, zda je soubor sestavení pomocí PEReader třídy.

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.

Viz také