Class AssemblyDefinition
- Namespace
- AsmResolver.DotNet
- Assembly
- AsmResolver.DotNet.dll
Represents an assembly of self-describing modules of an executable file hosted by a common language runtime (CLR).
public class AssemblyDefinition : AssemblyDescriptor, IHasCustomAttribute, IFullNameProvider, INameProvider, IImportable, IModuleProvider, IHasSecurityDeclaration, IMetadataMember
- Inheritance
-
AssemblyDefinition
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
AssemblyDefinition(MetadataToken)
Initializes a new assembly definition.
protected AssemblyDefinition(MetadataToken token)
Parameters
token
MetadataTokenThe token of the assembly definition.
AssemblyDefinition(Utf8String?, Version)
Creates a new assembly definition.
public AssemblyDefinition(Utf8String? name, Version version)
Parameters
name
Utf8StringThe name of the assembly.
version
VersionThe version of the assembly.
Properties
HasManifestModule
Gets a value indicating whether the assembly contains a manifest module.
public bool HasManifestModule { get; }
Property Value
HashAlgorithm
Gets or sets the hashing algorithm that is used to sign the assembly.
public AssemblyHashAlgorithm HashAlgorithm { get; set; }
Property Value
IsCorLib
Gets a value indicating whether the assembly descriptor references a Common Object Runtime standard library.
public override bool IsCorLib { get; }
Property Value
ManifestModule
Gets the main module of the .NET assembly containing the assembly's manifest.
public ModuleDefinition? ManifestModule { get; }
Property Value
Modules
Gets a collection of modules that this .NET assembly defines.
public IList<ModuleDefinition> Modules { get; }
Property Value
PublicKey
Gets or sets the public key of the assembly to use for verification of a signature.
public byte[]? PublicKey { get; set; }
Property Value
- byte[]
Remarks
If this value is set to null
, no public key will be assigned.
This property does not automatically update the HasPublicKey property.
This property corresponds to the Culture column in the assembly definition table.
SecurityDeclarations
Gets a collection of security declarations assigned to the member.
public IList<SecurityDeclaration> SecurityDeclarations { get; }
Property Value
Methods
FromBytes(byte[])
Reads a .NET assembly from the provided input buffer.
public static AssemblyDefinition FromBytes(byte[] buffer)
Parameters
buffer
byte[]The raw contents of the executable file to load.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromBytes(byte[], ModuleReaderParameters)
Reads a .NET assembly from the provided input buffer.
public static AssemblyDefinition FromBytes(byte[] buffer, ModuleReaderParameters readerParameters)
Parameters
buffer
byte[]The raw contents of the executable file to load.
readerParameters
ModuleReaderParametersThe parameters to use while reading the assembly.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromFile(IInputFile)
Reads a .NET assembly from the provided input file.
public static AssemblyDefinition FromFile(IInputFile file)
Parameters
file
IInputFileThe portable executable file to load.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromFile(IInputFile, ModuleReaderParameters)
Reads a .NET assembly from the provided input file.
public static AssemblyDefinition FromFile(IInputFile file, ModuleReaderParameters readerParameters)
Parameters
file
IInputFileThe portable executable file to load.
readerParameters
ModuleReaderParametersThe parameters to use while reading the assembly.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromFile(PEFile)
Reads a .NET assembly from the provided input file.
public static AssemblyDefinition FromFile(PEFile file)
Parameters
file
PEFileThe portable executable file to load.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromFile(PEFile, ModuleReaderParameters)
Reads a .NET assembly from the provided input file.
public static AssemblyDefinition FromFile(PEFile file, ModuleReaderParameters readerParameters)
Parameters
file
PEFileThe portable executable file to load.
readerParameters
ModuleReaderParametersThe parameters to use while reading the assembly.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromFile(string)
Reads a .NET assembly from the provided input file.
public static AssemblyDefinition FromFile(string filePath)
Parameters
filePath
stringThe file path to the input executable to load.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromFile(string, ModuleReaderParameters)
Reads a .NET assembly from the provided input file.
public static AssemblyDefinition FromFile(string filePath, ModuleReaderParameters readerParameters)
Parameters
filePath
stringThe file path to the input executable to load.
readerParameters
ModuleReaderParametersThe parameters to use while reading the assembly.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromImage(PEImage)
Initializes a .NET assembly from a PE image.
public static AssemblyDefinition FromImage(PEImage peImage)
Parameters
peImage
PEImageThe image containing the .NET metadata.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromImage(PEImage, ModuleReaderParameters)
Initializes a .NET assembly from a PE image.
public static AssemblyDefinition FromImage(PEImage peImage, ModuleReaderParameters readerParameters)
Parameters
peImage
PEImageThe image containing the .NET metadata.
readerParameters
ModuleReaderParametersThe parameters to use while reading the assembly.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
FromReader(in BinaryStreamReader, PEMappingMode)
Reads a .NET assembly from an input stream.
public static AssemblyDefinition FromReader(in BinaryStreamReader reader, PEMappingMode mode = PEMappingMode.Unmapped)
Parameters
reader
BinaryStreamReaderThe input stream pointing at the beginning of the executable to load.
mode
PEMappingModeIndicates the input PE is mapped or unmapped.
Returns
- AssemblyDefinition
The module.
Exceptions
- BadImageFormatException
Occurs when the image does not contain a valid .NET metadata directory.
GetModules()
Obtains the list of defined modules in the .NET assembly.
protected virtual IList<ModuleDefinition> GetModules()
Returns
- IList<ModuleDefinition>
The modules.
Remarks
This method is called upon initializing the Modules and/or ManifestModule property.
GetPublicKey()
Obtains the public key of the assembly definition.
protected virtual byte[]? GetPublicKey()
Returns
- byte[]
The public key.
Remarks
This method is called upon initializing the PublicKey property.
GetPublicKeyToken()
When the application is signed with a strong name, obtains the public key token of the assembly
public override byte[]? GetPublicKeyToken()
Returns
- byte[]
The token.
GetSecurityDeclarations()
Obtains the list of security declarations assigned to the member.
protected virtual IList<SecurityDeclaration> GetSecurityDeclarations()
Returns
- IList<SecurityDeclaration>
The security declarations
Remarks
This method is called upon initialization of the SecurityDeclarations property.
ImportWith(ReferenceImporter)
Imports the assembly descriptor using the provided reference importer.
public override AssemblyReference ImportWith(ReferenceImporter importer)
Parameters
importer
ReferenceImporterThe importer object to use.
Returns
- AssemblyReference
The imported assembly reference.
IsImportedInModule(ModuleDefinition)
Determines whether the descriptor of the member is fully imported in the provided module.
public override bool IsImportedInModule(ModuleDefinition module)
Parameters
module
ModuleDefinitionThe module that is supposed to import the member.
Returns
- bool
true
if the descriptor of the member is fully imported by the module,false
otherwise.
Remarks
This method verifies all references in the descriptor of the member only. It does not verify any additional data or contents (such as a method body) associated to the member.
Resolve()
Resolves the reference to the assembly to an assembly definition.
public override AssemblyDefinition Resolve()
Returns
- AssemblyDefinition
The assembly definition, or
null
if the resolution failed.
TryGetTargetFramework(out DotNetRuntimeInfo)
Attempts to extract the target runtime from the TryGetTargetFramework(out DotNetRuntimeInfo) attribute.
public virtual bool TryGetTargetFramework(out DotNetRuntimeInfo info)
Parameters
info
DotNetRuntimeInfoThe runtime.
Returns
- bool
true
if the attribute was found and the runtime was extracted,false
otherwise.
Write(string)
Rebuilds the .NET assembly to a portable executable file and writes it to the file system.
public void Write(string filePath)
Parameters
filePath
stringThe output path of the manifest module file.
Write(string, IPEImageBuilder)
Rebuilds the .NET assembly to a portable executable file and writes it to the file system.
public void Write(string filePath, IPEImageBuilder imageBuilder)
Parameters
filePath
stringThe output path of the manifest module file.
imageBuilder
IPEImageBuilderThe engine to use for reconstructing a PE image.
Write(string, IPEImageBuilder, IPEFileBuilder)
Rebuilds the .NET assembly to a portable executable file and writes it to the file system.
public void Write(string filePath, IPEImageBuilder imageBuilder, IPEFileBuilder fileBuilder)
Parameters
filePath
stringThe output path of the manifest module file.
imageBuilder
IPEImageBuilderThe engine to use for reconstructing a PE image.
fileBuilder
IPEFileBuilderThe engine to use for reconstructing a PE file.
WriteManifest(Stream)
Rebuilds the manifest module and writes it to the stream specified.
public void WriteManifest(Stream stream)
Parameters
stream
StreamThe output stream of the manifest module file.
WriteManifest(Stream, IPEImageBuilder)
Rebuilds the manifest module and writes it to the stream specified.
public void WriteManifest(Stream stream, IPEImageBuilder imageBuilder)
Parameters
stream
StreamThe output stream of the manifest module file.
imageBuilder
IPEImageBuilderThe engine to use for reconstructing a PE image.
WriteManifest(Stream, IPEImageBuilder, IPEFileBuilder)
Rebuilds the manifest module and writes it to the stream specified.
public void WriteManifest(Stream stream, IPEImageBuilder imageBuilder, IPEFileBuilder fileBuilder)
Parameters
stream
StreamThe output stream of the manifest module file.
imageBuilder
IPEImageBuilderThe engine to use for reconstructing a PE image.
fileBuilder
IPEFileBuilderThe engine to use for reconstructing a PE file.