// dnlib: See LICENSE.txt for more info
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using dnlib.DotNet.MD;
using dnlib.DotNet.Pdb;
namespace dnlib.DotNet {
///
/// A high-level representation of a row in the StandAloneSig table
///
public abstract class StandAloneSig : IHasCustomAttribute, IHasCustomDebugInformation, IContainsGenericParameter {
///
/// The row id in its table
///
protected uint rid;
///
public MDToken MDToken => new MDToken(Table.StandAloneSig, rid);
///
public uint Rid {
get => rid;
set => rid = value;
}
///
public int HasCustomAttributeTag => 11;
///
/// From column StandAloneSig.Signature
///
public CallingConventionSig Signature {
get => signature;
set => signature = value;
}
///
protected CallingConventionSig signature;
///
/// Gets all custom attributes
///
public CustomAttributeCollection CustomAttributes {
get {
if (customAttributes is null)
InitializeCustomAttributes();
return customAttributes;
}
}
///
protected CustomAttributeCollection customAttributes;
/// Initializes
protected virtual void InitializeCustomAttributes() =>
Interlocked.CompareExchange(ref customAttributes, new CustomAttributeCollection(), null);
///
public bool HasCustomAttributes => CustomAttributes.Count > 0;
///
public int HasCustomDebugInformationTag => 11;
///
public bool HasCustomDebugInfos => CustomDebugInfos.Count > 0;
///
/// Gets all custom debug infos
///
public IList CustomDebugInfos {
get {
if (customDebugInfos is null)
InitializeCustomDebugInfos();
return customDebugInfos;
}
}
///
protected IList customDebugInfos;
/// Initializes
protected virtual void InitializeCustomDebugInfos() =>
Interlocked.CompareExchange(ref customDebugInfos, new List(), null);
///
/// Gets/sets the method sig
///
public MethodSig MethodSig {
get => signature as MethodSig;
set => signature = value;
}
///
/// Gets/sets the locals sig
///
public LocalSig LocalSig {
get => signature as LocalSig;
set => signature = value;
}
///
public bool ContainsGenericParameter => TypeHelper.ContainsGenericParameter(this);
}
///
/// A StandAloneSig row created by the user and not present in the original .NET file
///
public class StandAloneSigUser : StandAloneSig {
///
/// Default constructor
///
public StandAloneSigUser() {
}
///
/// Constructor
///
/// A locals sig
public StandAloneSigUser(LocalSig localSig) => signature = localSig;
///
/// Constructor
///
/// A method sig
public StandAloneSigUser(MethodSig methodSig) => signature = methodSig;
}
///
/// Created from a row in the StandAloneSig table
///
sealed class StandAloneSigMD : StandAloneSig, IMDTokenProviderMD, IContainsGenericParameter2 {
/// The module where this instance is located
readonly ModuleDefMD readerModule;
readonly uint origRid;
readonly GenericParamContext gpContext;
///
public uint OrigRid => origRid;
///
protected override void InitializeCustomAttributes() {
var list = readerModule.Metadata.GetCustomAttributeRidList(Table.StandAloneSig, origRid);
var tmp = new CustomAttributeCollection(list.Count, list, (list2, index) => readerModule.ReadCustomAttribute(list[index]));
Interlocked.CompareExchange(ref customAttributes, tmp, null);
}
///
protected override void InitializeCustomDebugInfos() {
var list = new List();
readerModule.InitializeCustomDebugInfos(new MDToken(MDToken.Table, origRid), gpContext, list);
Interlocked.CompareExchange(ref customDebugInfos, list, null);
}
///
/// Constructor
///
/// The module which contains this StandAloneSig row
/// Row ID
/// Generic parameter context
/// If is null
/// If is invalid
public StandAloneSigMD(ModuleDefMD readerModule, uint rid, GenericParamContext gpContext) {
#if DEBUG
if (readerModule is null)
throw new ArgumentNullException("readerModule");
if (readerModule.TablesStream.StandAloneSigTable.IsInvalidRID(rid))
throw new BadImageFormatException($"StandAloneSig rid {rid} does not exist");
#endif
origRid = rid;
this.rid = rid;
this.readerModule = readerModule;
this.gpContext = gpContext;
bool b = readerModule.TablesStream.TryReadStandAloneSigRow(origRid, out var row);
Debug.Assert(b);
signature = readerModule.ReadSignature(row.Signature, gpContext);
}
}
}