如何:确定文件是否为程序集

当且仅当程序集处于托管状态,并在其元数据中包含程序集条目时,该文件才为程序集。 有关程序集和元数据的详细信息,请参阅程序集清单

如何手动确定文件是否为程序集

  1. 启动 Ildasm.exe(IL 反汇编程序)工具。

  2. 加载要测试的文件。

  3. 如果 ILDASM 报告文件不是可移植的可执行 (PE) 文件,则不是程序集。 有关详细信息,请参阅主题如何:查看程序集内容

如何以编程方式确定文件是否为程序集

使用 AssemblyName 类

  1. 调用 AssemblyName.GetAssemblyName 方法,传递要测试的文件的完整文件路径和名称。

  2. 如果引发 BadImageFormatException 异常,则该文件不是程序集。

此示例测试 DLL 以查看其是否为程序集。

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.  

GetAssemblyName 方法加载测试文件,然后在读取信息之后释放它。

使用 PEReader 类

  1. 如果面向 .NET Standard 或 .NET Framework,请安装 System.Reflection.Metadata NuGet 包。 (面向 .NET Core 或 .NET 5+ 时,不需要此步骤,因为此库包含在共享框架中。)

  2. 创建一个 System.IO.FileStream 实例,以便从要测试的文件中读取数据。

  3. 创建一个 System.Reflection.PortableExecutable.PEReader 实例,并将文件流传递到构造函数中。

  4. 检查 HasMetadata 属性的值。 如果该值为 false,则文件不为程序集。

  5. 在 PE 读取器实例上调用 GetMetadataReader 方法,以创建元数据读取器。

  6. 检查 IsAssembly 属性的值。 如果该值为 true,则文件为程序集。

GetAssemblyName 方法不同,PEReader 类不会在本地可移植可执行 (PE) 文件上引发异常。 这样能避免在需要检查此类文件时出现异常,从而省去性能方面额外的开销。 如果文件不存在或不是 PE 文件,仍需要处理异常。

此示例演示如何使用 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.

请参阅