// 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 InterfaceImpl table
///
[DebuggerDisplay("{Interface}")]
public abstract class InterfaceImpl : IHasCustomAttribute, IContainsGenericParameter, IHasCustomDebugInformation {
///
/// The row id in its table
///
protected uint rid;
///
public MDToken MDToken => new MDToken(Table.InterfaceImpl, rid);
///
public uint Rid {
get => rid;
set => rid = value;
}
///
public int HasCustomAttributeTag => 5;
///
/// From column InterfaceImpl.Interface
///
public ITypeDefOrRef Interface {
get => @interface;
set => @interface = value;
}
///
protected ITypeDefOrRef @interface;
///
/// 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 => 5;
///
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);
bool IContainsGenericParameter.ContainsGenericParameter => TypeHelper.ContainsGenericParameter(this);
}
///
/// An InterfaceImpl row created by the user and not present in the original .NET file
///
public class InterfaceImplUser : InterfaceImpl {
///
/// Default constructor
///
public InterfaceImplUser() {
}
///
/// Constructor
///
/// The interface the type implements
public InterfaceImplUser(ITypeDefOrRef @interface) => this.@interface = @interface;
}
///
/// Created from a row in the InterfaceImpl table
///
sealed class InterfaceImplMD : InterfaceImpl, IMDTokenProviderMD, IContainsGenericParameter2 {
/// The module where this instance is located
readonly ModuleDefMD readerModule;
readonly uint origRid;
readonly GenericParamContext gpContext;
///
public uint OrigRid => origRid;
bool IContainsGenericParameter2.ContainsGenericParameter => TypeHelper.ContainsGenericParameter(this);
///
protected override void InitializeCustomAttributes() {
var list = readerModule.Metadata.GetCustomAttributeRidList(Table.InterfaceImpl, 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 InterfaceImpl row
/// Row ID
/// Generic parameter context
/// If is null
/// If is invalid
public InterfaceImplMD(ModuleDefMD readerModule, uint rid, GenericParamContext gpContext) {
#if DEBUG
if (readerModule is null)
throw new ArgumentNullException("readerModule");
if (readerModule.TablesStream.InterfaceImplTable.IsInvalidRID(rid))
throw new BadImageFormatException($"InterfaceImpl rid {rid} does not exist");
#endif
origRid = rid;
this.rid = rid;
this.readerModule = readerModule;
this.gpContext = gpContext;
bool b = readerModule.TablesStream.TryReadInterfaceImplRow(origRid, out var row);
Debug.Assert(b);
@interface = readerModule.ResolveTypeDefOrRef(row.Interface, gpContext);
}
}
}