// dnlib: See LICENSE.txt for more info using System.Collections.Generic; using dnlib.Utils; using System; namespace dnlib.DotNet { /// /// Stores s /// public class CustomAttributeCollection : LazyList { /// /// Default constructor /// public CustomAttributeCollection() { } /// /// Constructor /// /// Initial length of the list /// Context passed to /// Delegate instance that returns original values public CustomAttributeCollection(int length, object context, Func readOriginalValue) : base(length, context, readOriginalValue) { } /// /// Checks whether a custom attribute is present /// /// Full name of custom attribute type /// true if the custom attribute type is present, false otherwise public bool IsDefined(string fullName) => Find(fullName) is not null; /// /// Removes all custom attributes of a certain type /// /// Full name of custom attribute type that should be removed public void RemoveAll(string fullName) { if (fullName == null) return; for (int i = Count - 1; i >= 0; i--) { var ca = this[i]; if (ca is not null && fullName.EndsWith(ca.TypeName, StringComparison.Ordinal) && ca.TypeFullName == fullName) RemoveAt(i); } } /// /// Finds a custom attribute /// /// Full name of custom attribute type /// A or null if it wasn't found public CustomAttribute Find(string fullName) { if (fullName == null) return null; foreach (var ca in this) { if (ca is not null && fullName.EndsWith(ca.TypeName, StringComparison.Ordinal) && ca.TypeFullName == fullName) return ca; } return null; } /// /// Finds all custom attributes of a certain type /// /// Full name of custom attribute type /// All s of the requested type public IEnumerable FindAll(string fullName) { if (fullName == null) yield break; foreach (var ca in this) { if (ca is not null && fullName.EndsWith(ca.TypeName, StringComparison.Ordinal) && ca.TypeFullName == fullName) yield return ca; } } /// /// Finds a custom attribute /// /// Custom attribute type /// The first found or null if none found public CustomAttribute Find(IType attrType) => Find(attrType, 0); /// /// Finds a custom attribute /// /// Custom attribute type /// Attribute type comparison flags /// The first found or null if none found public CustomAttribute Find(IType attrType, SigComparerOptions options) { var comparer = new SigComparer(options); foreach (var ca in this) { if (comparer.Equals(ca.AttributeType, attrType)) return ca; } return null; } /// /// Finds all custom attributes of a certain type /// /// Custom attribute type /// All s of the requested type public IEnumerable FindAll(IType attrType) => FindAll(attrType, 0); /// /// Finds all custom attributes of a certain type /// /// Custom attribute type /// Attribute type comparison flags /// All s of the requested type public IEnumerable FindAll(IType attrType, SigComparerOptions options) { var comparer = new SigComparer(options); foreach (var ca in this) { if (comparer.Equals(ca.AttributeType, attrType)) yield return ca; } } } }