Catatan
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba masuk atau mengubah direktori.
Akses ke halaman ini memerlukan otorisasi. Anda dapat mencoba mengubah direktori.
Sebuah file adalah "assembly" jika dan hanya jika file tersebut dikelola, dan berisi entri "assembly" di metadatanya. Untuk informasi selengkapnya tentang rakitan dan metadata, lihat Manifes perakitan.
Cara menentukan secara manual apakah file adalah assembly
Mulai alat Ildasm.exe (Il Disassembler).
Muat file yang ingin Anda uji.
Jika ILDASM melaporkan bahwa file tersebut bukan file portable executable (PE), maka file tersebut bukan assembly. Untuk informasi selengkapnya, lihat topik Cara: Menampilkan konten rakitan.
Cara menentukan melalui pemrograman apakah sebuah file adalah assembly
Menggunakan kelas AssemblyName
Panggil metode AssemblyName.GetAssemblyName, dengan melewatkan path dan nama lengkap file yang Anda uji.
Jika sebuah pengecualian BadImageFormatException dilemparkan, file tersebut bukanlah sebuah assembly.
Contoh ini menguji DLL untuk melihat apakah itu adalah assemblasi.
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.
Metode memuat file GetAssemblyName pengujian, lalu merilisnya setelah informasi terbaca.
Menggunakan kelas PEReader
Perhatian
PEReader dan System.Reflection.Metadata pustaka tidak dirancang untuk menangani input yang tidak tepercaya. File PE yang salah bentuk atau berbahaya dapat menyebabkan perilaku tak terduga, termasuk akses memori di luar batas, crash, atau macet. Hanya gunakan API ini dengan modul tepercaya.
Jika Anda menargetkan .NET Standard atau .NET Framework, instal paket System.Reflection.Metadata NuGet. (Saat menargetkan .NET Core atau .NET 5+, langkah ini tidak diperlukan karena pustaka ini disertakan dalam kerangka kerja bersama.)
Buat System.IO.FileStream instans untuk membaca data dari file yang Sedang Anda uji.
Buat System.Reflection.PortableExecutable.PEReader instance, dengan meneruskan aliran file Anda ke konstruktor.
Periksa nilai HasMetadata properti . Jika nilainya adalah
false, file bukan assembly.GetMetadataReader Panggil metode pada instans pembaca PE untuk membuat pembaca metadata.
Periksa nilai IsAssembly properti . Jika nilainya adalah
true, file tersebut merupakan assembly.
Tidak seperti metode GetAssemblyName, kelas PEReader tidak melemparkan pengecualian pada file Portable Executable (PE) native. Ini memungkinkan Anda untuk menghindari biaya performa tambahan yang disebabkan oleh pengecualian ketika Anda perlu memeriksa file tersebut. Anda masih perlu menangani pengecualian jika file tidak ada atau bukan file PE.
Contoh ini menunjukkan cara menentukan apakah file adalah rakitan menggunakan PEReader kelas .
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.