方法: ファイルがアセンブリであるかどうかを確認する

ファイルが管理されていて、ファイルのメタデータにアセンブリ エントリが含まれている場合、そのファイルはアセンブリです。 アセンブリとメタデータの詳細については、「アセンブリ マニフェスト」を参照してください。

ファイルがアセンブリかどうかを手動で確認する方法

  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.

関連項目