Comment : déterminer si un fichier est un assembly

Un fichier est un assembly si et seulement s’il est managé et s’il contient une entrée d’assembly dans ses métadonnées. Pour plus d’informations sur les assemblys et les métadonnées, consultez Manifeste d’assembly.

Comment déterminer manuellement si un fichier est un assembly

  1. Démarrez Ildasm.exe (IL Disassembler).

  2. Chargez le fichier que vous souhaitez tester.

  3. Si ILDASM indique que le fichier n’est pas un fichier exécutable portable (PE), alors il ne s’agit pas d’un assembly. Pour plus d’informations, consultez la rubrique Guide pratique pour afficher le contenu d’un assembly.

Comment déterminer par programmation si un fichier est un assembly

Utilisation de la classe AssemblyName

  1. Appelez la méthode AssemblyName.GetAssemblyName en passant le chemin complet et le nom du fichier que vous testez.

  2. Si une exception BadImageFormatException est levée, le fichier n’est pas un assembly.

Cet exemple teste une DLL pour voir s’il s’agit d’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.  

La méthode GetAssemblyName charge le fichier de test, puis le libère une fois que les informations sont lues.

Utilisation de la classe PEReader

  1. Si vous ciblez .NET Standard ou .NET Framework, installez le package NuGet System.Reflection.Metadata. (Lorsque vous ciblez .NET Core ou .NET 5+, cette étape n’est pas obligatoire, car cette bibliothèque est incluse dans l’infrastructure partagée.)

  2. Créez une instance System.IO.FileStream pour lire les données à partir du fichier que vous testez.

  3. Créez une instance System.Reflection.PortableExecutable.PEReader, en passant votre flux de fichiers dans le constructeur.

  4. Vérifiez la valeur de la propriété HasMetadata. Si la valeur est false, le fichier n’est pas un assembly.

  5. Appelez la méthode GetMetadataReader sur l’instance de lecteur PE pour créer un lecteur de métadonnées.

  6. Vérifiez la valeur de la propriété IsAssembly. Si la valeur est true, le fichier est un assembly.

Contrairement à la méthode GetAssemblyName, la classe PEReader ne lève pas d’exception sur les fichiers exécutables portables natifs (PE). Cela vous permet d’éviter les coûts de performances supplémentaires causés par des exceptions lorsque vous devez vérifier ces fichiers. Vous devez toujours gérer les exceptions si le fichier n’existe pas ou s’il ne s’agit pas d’un fichier PE.

Cet exemple montre comment déterminer si un fichier est un assembly à l’aide de la classe PEReader.

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.

Voir aussi