Nasıl yapılır: Dosyanın bir derleme olup olmadığını belirleme
Bir dosya, yalnızca yönetiliyorsa bir derlemedir ve meta verilerinde bir derleme girdisi içerir. Derlemeler ve meta veriler hakkında daha fazla bilgi için bkz . Derleme bildirimi.
Bir dosyanın derleme olup olmadığını el ile belirleme
Ildasm.exe (IL Disassembler) aracını başlatın.
Test etmek istediğiniz dosyayı yükleyin.
ILDASM dosyanın taşınabilir yürütülebilir dosya (PE) olmadığını bildirirse, bu bir derleme değildir. Daha fazla bilgi için Nasıl yapılır: Derleme içeriğini görüntüleme konusuna bakın.
Bir dosyanın derleme olup olmadığını program aracılığıyla belirleme
AssemblyName sınıfını kullanma
AssemblyName.GetAssemblyName Tam dosya yolunu ve test ettiğiniz dosyanın adını geçirerek yöntemini çağırın.
Özel durum BadImageFormatException oluşturulursa, dosya bir derleme değildir.
Bu örnek bir DLL'nin derleme olup olmadığını görmek için sınar.
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 yöntemi test dosyasını yükler ve bilgiler okunduktan sonra serbest bırakır.
PEReader sınıfını kullanma
.NET Standard veya .NET Framework'ü hedef alıyorsanız System.Düşünceler yükleyin Iyon. Meta veri NuGet paketi. (.NET Core veya .NET 5+ hedeflenirken, bu kitaplık paylaşılan çerçeveye dahil edildiğinden bu adım gerekli değildir.)
Test ettiğiniz dosyadan verileri okumak için bir System.IO.FileStream örnek oluşturun.
Dosya akışınızı oluşturucuya geçirerek bir System.Reflection.PortableExecutable.PEReader örnek oluşturun.
özelliğinin HasMetadata değerini denetleyin. değer ise
false
, dosya bir derleme değildir.GetMetadataReader Meta veri okuyucu oluşturmak için PE okuyucu örneğinde yöntemini çağırın.
özelliğinin IsAssembly değerini denetleyin. değer ise
true
, dosya bir derlemedir.
yönteminden GetAssemblyName farklı olarak, PEReader sınıfı yerel Taşınabilir Yürütülebilir Dosya (PE) dosyalarında bir özel durum oluşturmaz. Bu, bu tür dosyaları denetlemeniz gerektiğinde özel durumların neden olduğu ek performans maliyetini önlemenizi sağlar. Dosyanın mevcut olmaması veya PE dosyası olmaması durumunda özel durumları işlemeniz gerekir.
Bu örnekte, bir dosyanın sınıfını kullanan bir derleme olup olmadığının nasıl belirleneceği gösterilmektedir 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.