/* 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; namespace dnSpy.Debugger.DotNet.Metadata { /// /// A .NET field /// public abstract class DmdFieldInfo : DmdMemberInfo, IEquatable { /// /// Gets the AppDomain /// public override DmdAppDomain AppDomain => DeclaringType!.AppDomain; /// /// Gets the member type /// public sealed override DmdMemberTypes MemberType => DmdMemberTypes.Field; /// /// Gets the field type /// public abstract DmdType FieldType { get; } /// /// Gets the field attributes /// public abstract DmdFieldAttributes Attributes { get; } #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public bool IsPublic => (Attributes & DmdFieldAttributes.FieldAccessMask) == DmdFieldAttributes.Public; public bool IsPrivate => (Attributes & DmdFieldAttributes.FieldAccessMask) == DmdFieldAttributes.Private; public bool IsFamily => (Attributes & DmdFieldAttributes.FieldAccessMask) == DmdFieldAttributes.Family; public bool IsAssembly => (Attributes & DmdFieldAttributes.FieldAccessMask) == DmdFieldAttributes.Assembly; public bool IsFamilyAndAssembly => (Attributes & DmdFieldAttributes.FieldAccessMask) == DmdFieldAttributes.FamANDAssem; public bool IsFamilyOrAssembly => (Attributes & DmdFieldAttributes.FieldAccessMask) == DmdFieldAttributes.FamORAssem; public bool IsPrivateScope => (Attributes & DmdFieldAttributes.FieldAccessMask) == DmdFieldAttributes.PrivateScope; public bool IsStatic => (Attributes & DmdFieldAttributes.Static) != 0; public bool IsInitOnly => (Attributes & DmdFieldAttributes.InitOnly) != 0; public bool IsLiteral => (Attributes & DmdFieldAttributes.Literal) != 0; public bool IsNotSerialized => (Attributes & DmdFieldAttributes.NotSerialized) != 0; public bool IsSpecialName => (Attributes & DmdFieldAttributes.SpecialName) != 0; public bool IsPinvokeImpl => (Attributes & DmdFieldAttributes.PinvokeImpl) != 0; public bool IsRTSpecialName => (Attributes & DmdFieldAttributes.RTSpecialName) != 0; public bool HasFieldMarshal => (Attributes & DmdFieldAttributes.HasFieldMarshal) != 0; public bool HasDefault => (Attributes & DmdFieldAttributes.HasDefault) != 0; public bool HasFieldRVA => (Attributes & DmdFieldAttributes.HasFieldRVA) != 0; #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member /// /// Gets the RVA of the data if is true /// public abstract uint FieldRVA { get; } /// /// Resolves a field reference /// /// true to throw if it doesn't exist, false to return null if it doesn't exist /// public sealed override DmdMemberInfo? ResolveMember(bool throwOnError) => Resolve(throwOnError); /// /// Resolves a field reference and throws if it doesn't exist /// /// public DmdFieldInfo Resolve() => Resolve(throwOnError: true)!; /// /// Resolves a field reference and returns null if it doesn't exist /// /// public DmdFieldInfo? ResolveNoThrow() => Resolve(throwOnError: false); /// /// Resolves a field reference /// /// true to throw if it doesn't exist, false to return null if it doesn't exist /// public abstract DmdFieldInfo? Resolve(bool throwOnError); /// /// Gets all required custom modifiers /// /// public DmdType[] GetRequiredCustomModifiers() => DmdCustomModifierUtilities.GetModifiers(GetCustomModifiers(), requiredModifiers: true); /// /// Gets all optional custom modifiers /// /// public DmdType[] GetOptionalCustomModifiers() => DmdCustomModifierUtilities.GetModifiers(GetCustomModifiers(), requiredModifiers: false); /// /// Gets all custom modifiers /// /// public ReadOnlyCollection GetCustomModifiers() => FieldType.GetCustomModifiers(); /// /// Gets the constant value stored in metadata if any exists /// /// public abstract object? GetRawConstantValue(); /// /// Gets the current value /// /// Evaluation context /// Instance or null if it's a static field /// public abstract object? GetValue(object? context, object? obj); /// /// Sets a new value /// /// Evaluation context /// Instance or null if it's a static field /// New value /// Binding attributes public abstract void SetValue(object? context, object? obj, object? value, DmdBindingFlags invokeAttr); /// /// Sets a new value /// /// Evaluation context /// Instance or null if it's a static field /// New value public void SetValue(object? context, object? obj, object? value) => SetValue(context, obj, value, DmdBindingFlags.Default); #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public static bool operator ==(DmdFieldInfo? left, DmdFieldInfo? right) => DmdMemberInfoEqualityComparer.DefaultMember.Equals(left, right); public static bool operator !=(DmdFieldInfo? left, DmdFieldInfo? right) => !DmdMemberInfoEqualityComparer.DefaultMember.Equals(left, right); #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member /// /// Equals() /// /// /// public bool Equals(DmdFieldInfo? other) => DmdMemberInfoEqualityComparer.DefaultMember.Equals(this, other); /// /// Equals() /// /// /// public override bool Equals(object? obj) => Equals(obj as DmdFieldInfo); /// /// GetHashCode() /// /// public override int GetHashCode() => DmdMemberInfoEqualityComparer.DefaultMember.GetHashCode(this); /// /// ToString() /// /// public sealed override string? ToString() => DmdMemberFormatter.Format(this); } }