// dnlib: See LICENSE.txt for more info
using System;
using System.Reflection;
namespace dnlib.DotNet {
///
/// Stores assembly name information
///
public sealed class AssemblyNameInfo : IAssembly {
AssemblyHashAlgorithm hashAlgId;
Version version;
AssemblyAttributes flags;
PublicKeyBase publicKeyOrToken;
UTF8String name;
UTF8String culture;
///
/// Gets/sets the
///
public AssemblyHashAlgorithm HashAlgId {
get => hashAlgId;
set => hashAlgId = value;
}
///
/// Gets/sets the or null if none specified
///
public Version Version {
get => version;
set => version = value;
}
///
/// Gets/sets the
///
public AssemblyAttributes Attributes {
get => flags;
set => flags = value;
}
///
/// Gets/sets the public key or token
///
public PublicKeyBase PublicKeyOrToken {
get => publicKeyOrToken;
set => publicKeyOrToken = value;
}
///
/// Gets/sets the name
///
public UTF8String Name {
get => name;
set => name = value;
}
///
/// Gets/sets the culture or null if none specified
///
public UTF8String Culture {
get => culture;
set => culture = value;
}
///
/// Gets the full name of the assembly
///
public string FullName => FullNameToken;
///
/// Gets the full name of the assembly but use a public key token
///
public string FullNameToken => FullNameFactory.AssemblyFullName(this, true);
///
/// Modify property: =
/// ( & ) | .
///
/// Value to AND
/// Value to OR
void ModifyAttributes(AssemblyAttributes andMask, AssemblyAttributes orMask) => Attributes = (Attributes & andMask) | orMask;
///
/// Set or clear flags in
///
/// true if flags should be set, false if flags should
/// be cleared
/// Flags to set or clear
void ModifyAttributes(bool set, AssemblyAttributes flags) {
if (set)
Attributes |= flags;
else
Attributes &= ~flags;
}
///
/// Gets/sets the bit
///
public bool HasPublicKey {
get => (Attributes & AssemblyAttributes.PublicKey) != 0;
set => ModifyAttributes(value, AssemblyAttributes.PublicKey);
}
///
/// Gets/sets the processor architecture
///
public AssemblyAttributes ProcessorArchitecture {
get => Attributes & AssemblyAttributes.PA_Mask;
set => ModifyAttributes(~AssemblyAttributes.PA_Mask, value & AssemblyAttributes.PA_Mask);
}
///
/// Gets/sets the processor architecture
///
public AssemblyAttributes ProcessorArchitectureFull {
get => Attributes & AssemblyAttributes.PA_FullMask;
set => ModifyAttributes(~AssemblyAttributes.PA_FullMask, value & AssemblyAttributes.PA_FullMask);
}
///
/// true if unspecified processor architecture
///
public bool IsProcessorArchitectureNone => (Attributes & AssemblyAttributes.PA_Mask) == AssemblyAttributes.PA_None;
///
/// true if neutral (PE32) architecture
///
public bool IsProcessorArchitectureMSIL => (Attributes & AssemblyAttributes.PA_Mask) == AssemblyAttributes.PA_MSIL;
///
/// true if x86 (PE32) architecture
///
public bool IsProcessorArchitectureX86 => (Attributes & AssemblyAttributes.PA_Mask) == AssemblyAttributes.PA_x86;
///
/// true if IA-64 (PE32+) architecture
///
public bool IsProcessorArchitectureIA64 => (Attributes & AssemblyAttributes.PA_Mask) == AssemblyAttributes.PA_IA64;
///
/// true if x64 (PE32+) architecture
///
public bool IsProcessorArchitectureX64 => (Attributes & AssemblyAttributes.PA_Mask) == AssemblyAttributes.PA_AMD64;
///
/// true if ARM (PE32) architecture
///
public bool IsProcessorArchitectureARM => (Attributes & AssemblyAttributes.PA_Mask) == AssemblyAttributes.PA_ARM;
///
/// true if eg. reference assembly (not runnable)
///
public bool IsProcessorArchitectureNoPlatform => (Attributes & AssemblyAttributes.PA_Mask) == AssemblyAttributes.PA_NoPlatform;
///
/// Gets/sets the bit
///
public bool IsProcessorArchitectureSpecified {
get => (Attributes & AssemblyAttributes.PA_Specified) != 0;
set => ModifyAttributes(value, AssemblyAttributes.PA_Specified);
}
///
/// Gets/sets the bit
///
public bool EnableJITcompileTracking {
get => (Attributes & AssemblyAttributes.EnableJITcompileTracking) != 0;
set => ModifyAttributes(value, AssemblyAttributes.EnableJITcompileTracking);
}
///
/// Gets/sets the bit
///
public bool DisableJITcompileOptimizer {
get => (Attributes & AssemblyAttributes.DisableJITcompileOptimizer) != 0;
set => ModifyAttributes(value, AssemblyAttributes.DisableJITcompileOptimizer);
}
///
/// Gets/sets the bit
///
public bool IsRetargetable {
get => (Attributes & AssemblyAttributes.Retargetable) != 0;
set => ModifyAttributes(value, AssemblyAttributes.Retargetable);
}
///
/// Gets/sets the content type
///
public AssemblyAttributes ContentType {
get => Attributes & AssemblyAttributes.ContentType_Mask;
set => ModifyAttributes(~AssemblyAttributes.ContentType_Mask, value & AssemblyAttributes.ContentType_Mask);
}
///
/// true if content type is Default
///
public bool IsContentTypeDefault => (Attributes & AssemblyAttributes.ContentType_Mask) == AssemblyAttributes.ContentType_Default;
///
/// true if content type is WindowsRuntime
///
public bool IsContentTypeWindowsRuntime => (Attributes & AssemblyAttributes.ContentType_Mask) == AssemblyAttributes.ContentType_WindowsRuntime;
///
/// Default constructor
///
public AssemblyNameInfo() {
}
///
/// Constructor
///
/// An assembly name
public AssemblyNameInfo(string asmFullName)
: this(ReflectionTypeNameParser.ParseAssemblyRef(asmFullName)) {
}
///
/// Constructor
///
/// The assembly
public AssemblyNameInfo(IAssembly asm) {
if (asm is null)
return;
var asmDef = asm as AssemblyDef;
hashAlgId = asmDef is null ? 0 : asmDef.HashAlgorithm;
version = asm.Version ?? new Version(0, 0, 0, 0);
flags = asm.Attributes;
publicKeyOrToken = asm.PublicKeyOrToken;
name = UTF8String.IsNullOrEmpty(asm.Name) ? UTF8String.Empty : asm.Name;
culture = UTF8String.IsNullOrEmpty(asm.Culture) ? UTF8String.Empty : asm.Culture;
}
///
/// Constructor
///
/// Assembly name info
public AssemblyNameInfo(AssemblyName asmName) {
if (asmName is null)
return;
hashAlgId = (AssemblyHashAlgorithm)asmName.HashAlgorithm;
version = asmName.Version ?? new Version(0, 0, 0, 0);
flags = (AssemblyAttributes)asmName.Flags;
publicKeyOrToken = (PublicKeyBase)PublicKeyBase.CreatePublicKey(asmName.GetPublicKey()) ??
PublicKeyBase.CreatePublicKeyToken(asmName.GetPublicKeyToken());
name = asmName.Name ?? string.Empty;
culture = asmName.CultureInfo is not null && asmName.CultureInfo.Name is not null ? asmName.CultureInfo.Name : string.Empty;
}
///
public override string ToString() => FullName;
}
}