/*
Copyright (C) 2014-2019 de4dot@gmail.com
This file is part of dnSpy
dnSpy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dnSpy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dnSpy. If not, see .
*/
using System;
using System.Collections.ObjectModel;
using dnSpy.Debugger.DotNet.Metadata.Impl;
namespace dnSpy.Debugger.DotNet.Metadata {
///
/// Base class of all types, fields, methods, constructors, properties, events
///
public abstract class DmdMemberInfo : DmdObject, IDmdCustomAttributeProvider, IDmdSecurityAttributeProvider, IEquatable {
///
/// Dummy abstract method to make sure no-one outside this assembly can create their own
///
private protected abstract void YouCantDeriveFromThisClass();
///
/// Resolves a member reference and throws if it doesn't exist
///
///
public DmdMemberInfo ResolveMember() => ResolveMember(throwOnError: true)!;
///
/// Resolves a member reference and returns null if it doesn't exist
///
///
public DmdMemberInfo? ResolveMemberNoThrow() => ResolveMember(throwOnError: false);
///
/// Resolves a member reference
///
/// true to throw if it doesn't exist, false to return null if it doesn't exist
///
public abstract DmdMemberInfo? ResolveMember(bool throwOnError);
///
/// Gets the AppDomain
///
public abstract DmdAppDomain AppDomain { get; }
///
/// Gets the member type
///
public abstract DmdMemberTypes MemberType { get; }
///
/// Gets the member name
///
public abstract string Name { get; }
///
/// Gets the declaring type. This is the type that declares the member, see also
///
public abstract DmdType? DeclaringType { get; }
///
/// Gets the reflected type. This is the type that owns this member, see also
///
public abstract DmdType? ReflectedType { get; }
///
/// Gets the metadata token
///
public abstract int MetadataToken { get; }
///
/// Gets the module
///
public abstract DmdModule Module { get; }
///
/// true if it's a reference to another type or member, eg. a TypeRef, MemberRef
///
public abstract bool IsMetadataReference { get; }
///
/// Checks if this instance and have the same metadata definition
///
/// Other member
///
public bool HasSameMetadataDefinitionAs(DmdMemberInfo other) {
if (other is null)
throw new ArgumentNullException(nameof(other));
return other.Module == Module && other.MetadataToken == MetadataToken && MetadataToken != 0;
}
///
/// Gets the security attributes
///
public ReadOnlyCollection SecurityAttributes => GetSecurityAttributesData();
///
/// Gets the security attributes
///
///
public virtual ReadOnlyCollection GetSecurityAttributesData() => ReadOnlyCollectionHelpers.Empty();
///
/// Gets the custom attributes
///
public ReadOnlyCollection CustomAttributes => GetCustomAttributesData();
///
/// Gets the custom attributes
///
///
public abstract ReadOnlyCollection GetCustomAttributesData();
///
/// Checks if a custom attribute is present
///
/// Full name of the custom attribute type
/// true to check custom attributes in all base classes
///
public virtual bool IsDefined(string attributeTypeFullName, bool inherit) => CustomAttributesHelper.IsDefined(GetCustomAttributesData(), attributeTypeFullName);
///
/// Checks if a custom attribute is present
///
/// Custom attribute type
/// true to check custom attributes in all base classes
///
public virtual bool IsDefined(DmdType? attributeType, bool inherit) => CustomAttributesHelper.IsDefined(GetCustomAttributesData(), attributeType);
///
/// Checks if a custom attribute is present
///
/// Custom attribute type
/// true to check custom attributes in all base classes
///
public virtual bool IsDefined(Type attributeType, bool inherit) => CustomAttributesHelper.IsDefined(GetCustomAttributesData(), DmdTypeUtilities.ToDmdType(attributeType, AppDomain));
///
/// Finds a custom attribute
///
/// Full name of the custom attribute type
/// true to check custom attributes in all base classes
///
public virtual DmdCustomAttributeData? FindCustomAttribute(string attributeTypeFullName, bool inherit) => CustomAttributesHelper.Find(GetCustomAttributesData(), attributeTypeFullName);
///
/// Finds a custom attribute
///
/// Custom attribute type
/// true to check custom attributes in all base classes
///
public virtual DmdCustomAttributeData? FindCustomAttribute(DmdType? attributeType, bool inherit) => CustomAttributesHelper.Find(GetCustomAttributesData(), attributeType);
///
/// Finds a custom attribute
///
/// Custom attribute type
/// true to check custom attributes in all base classes
///
public virtual DmdCustomAttributeData? FindCustomAttribute(Type attributeType, bool inherit) => CustomAttributesHelper.Find(GetCustomAttributesData(), DmdTypeUtilities.ToDmdType(attributeType, AppDomain));
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static bool operator ==(DmdMemberInfo? left, DmdMemberInfo? right) => left is DmdType ? DmdMemberInfoEqualityComparer.DefaultType.Equals(left, right) : DmdMemberInfoEqualityComparer.DefaultMember.Equals(left, right);
public static bool operator !=(DmdMemberInfo? left, DmdMemberInfo? right) => !(left is DmdType ? DmdMemberInfoEqualityComparer.DefaultType.Equals(left, right) : DmdMemberInfoEqualityComparer.DefaultMember.Equals(left, right));
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
///
/// Equals()
///
///
///
public bool Equals(DmdMemberInfo? other) => this is DmdType ? DmdMemberInfoEqualityComparer.DefaultType.Equals(this, other) : DmdMemberInfoEqualityComparer.DefaultMember.Equals(this, other);
///
/// Equals()
///
///
///
public abstract override bool Equals(object? obj);
///
/// GetHashCode()
///
///
public abstract override int GetHashCode();
///
/// ToString()
///
///
public abstract override string? ToString();
}
}