From c1bb3268f80517e2a5b26964b079faabf507841f Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 06:38:40 -0700 Subject: [PATCH 01/11] Refactor runtime-specific logic out of CustomAttributeData CustomAttributeData is meant to be abstract base class of reflection object model. Move the runtime-specific part into a separate RuntimeCustomAttributeData type. --- .../System.Private.CoreLib.csproj | 2 +- .../src/System/Reflection/RuntimeAssembly.cs | 2 +- .../Reflection/RuntimeConstructorInfo.cs | 2 +- ...ibute.cs => RuntimeCustomAttributeData.cs} | 63 +++++++------------ .../src/System/Reflection/RuntimeEventInfo.cs | 2 +- .../src/System/Reflection/RuntimeFieldInfo.cs | 2 +- .../System/Reflection/RuntimeMethodInfo.cs | 2 +- .../src/System/Reflection/RuntimeModule.cs | 2 +- .../System/Reflection/RuntimeParameterInfo.cs | 6 +- .../System/Reflection/RuntimePropertyInfo.cs | 2 +- src/coreclr/vm/corelib.h | 10 +-- .../System/Reflection/CustomAttributeData.cs | 35 +++++++---- .../CustomAttributeNamedArgument.cs | 24 +++---- .../CustomAttributeTypedArgument.cs | 32 ++++++---- .../src/System/RuntimeType.cs | 2 +- .../System.Private.CoreLib.csproj | 3 +- .../src/System/Reflection/CustomAttribute.cs | 4 +- .../CustomAttributeTypedArgument.Mono.cs | 16 ----- .../src/System/Reflection/FieldInfo.Mono.cs | 6 +- .../src/System/Reflection/RuntimeAssembly.cs | 2 +- ....Mono.cs => RuntimeCustomAttributeData.cs} | 18 +++--- .../src/System/Reflection/RuntimeEventInfo.cs | 2 +- .../src/System/Reflection/RuntimeFieldInfo.cs | 2 +- .../System/Reflection/RuntimeMethodInfo.cs | 8 +-- .../src/System/Reflection/RuntimeModule.cs | 2 +- .../System/Reflection/RuntimeParameterInfo.cs | 10 +-- .../System/Reflection/RuntimePropertyInfo.cs | 2 +- src/mono/mono/metadata/custom-attrs.c | 6 +- src/mono/mono/metadata/icall-def-netcore.h | 4 +- 29 files changed, 129 insertions(+), 144 deletions(-) rename src/coreclr/System.Private.CoreLib/src/System/Reflection/{CustomAttribute.cs => RuntimeCustomAttributeData.cs} (97%) delete mode 100644 src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.Mono.cs rename src/mono/System.Private.CoreLib/src/System/Reflection/{CustomAttributeData.Mono.cs => RuntimeCustomAttributeData.cs} (92%) diff --git a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj index 1075a708b8bbb3..bc40c0e4830852 100644 --- a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -155,7 +155,6 @@ - @@ -194,6 +193,7 @@ + diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs index a45d81d4b2342a..cb7bd41495f6dc 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs @@ -322,7 +322,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } internal static RuntimeAssembly InternalLoad(string assemblyName, ref StackCrawlMark stackMark, AssemblyLoadContext? assemblyLoadContext = null) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.cs index 21bcf10557f9be..1af3d758916f0c 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeConstructorInfo.cs @@ -198,7 +198,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } #endregion diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs similarity index 97% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs rename to src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs index 96c062f4885dc6..6a90eb2ca7f960 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs @@ -12,7 +12,7 @@ namespace System.Reflection { - public partial class CustomAttributeData + public class RuntimeCustomAttributeData : CustomAttributeData { #region Internal Static Members internal static IList GetCustomAttributesInternal(RuntimeType target) @@ -103,7 +103,7 @@ private static IList GetCombinedList(IList GetCustomAttributes(RuntimeModule modu CustomAttributeData[] customAttributes = new CustomAttributeData[records.Length]; for (int i = 0; i < records.Length; i++) - customAttributes[i] = new CustomAttributeData(module, records[i].tkCtor, in records[i].blob); + customAttributes[i] = new RuntimeCustomAttributeData(module, records[i].tkCtor, in records[i].blob); return Array.AsReadOnly(customAttributes); } @@ -262,17 +262,13 @@ internal static CustomAttributeTypedArgument Filter(IList a private IList m_namedArgs = null!; #region Constructor - protected CustomAttributeData() - { - } - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern", Justification = "Property setters and fields which are accessed by any attribute instantiation which is present in the code linker has analyzed." + "As such enumerating all fields and properties may return different results after trimming" + "but all those which are needed to actually have data will be there.")] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", Justification = "We're getting a MethodBase of a constructor that we found in the metadata. The attribute constructor won't be trimmed.")] - private CustomAttributeData(RuntimeModule scope, MetadataToken caCtorToken, in ConstArray blob) + private RuntimeCustomAttributeData(RuntimeModule scope, MetadataToken caCtorToken, in ConstArray blob) { m_scope = scope; m_ctor = (RuntimeConstructorInfo)RuntimeType.GetMethodBase(scope, caCtorToken)!; @@ -301,7 +297,7 @@ private CustomAttributeData(RuntimeModule scope, MetadataToken caCtorToken, in C #endregion #region Pseudo Custom Attribute Constructor - internal CustomAttributeData(Attribute attribute) + internal RuntimeCustomAttributeData(Attribute attribute) { if (attribute is DllImportAttribute dllImportAttribute) Init(dllImportAttribute); @@ -415,9 +411,9 @@ private void Init(object pca) #endregion #region Public Members - public virtual ConstructorInfo Constructor => m_ctor; + public override ConstructorInfo Constructor => m_ctor; - public virtual IList ConstructorArguments + public override IList ConstructorArguments { get { @@ -439,7 +435,7 @@ public virtual IList ConstructorArguments } } - public virtual IList NamedArguments + public override IList NamedArguments { get { @@ -557,17 +553,6 @@ private static RuntimeType ResolveType(RuntimeModule scope, string typeName) } #endregion - private static object CanonicalizeValue(object value) - { - Debug.Assert(value is not null); - - if (value.GetType().IsEnum) - { - return ((Enum)value).GetValue(); - } - return value; - } - internal CustomAttributeTypedArgument(RuntimeModule scope, CustomAttributeEncodedArgument encodedArg) { CustomAttributeEncoding encodedType = encodedArg.CustomAttributeType.EncodedType; @@ -577,22 +562,22 @@ internal CustomAttributeTypedArgument(RuntimeModule scope, CustomAttributeEncode if (encodedType == CustomAttributeEncoding.Enum) { - m_argumentType = ResolveType(scope, encodedArg.CustomAttributeType.EnumName!); - m_value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedArg.CustomAttributeType.EncodedEnumType); + _argumentType = ResolveType(scope, encodedArg.CustomAttributeType.EnumName!); + _value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedArg.CustomAttributeType.EncodedEnumType); } else if (encodedType == CustomAttributeEncoding.String) { - m_argumentType = typeof(string); - m_value = encodedArg.StringValue; + _argumentType = typeof(string); + _value = encodedArg.StringValue; } else if (encodedType == CustomAttributeEncoding.Type) { - m_argumentType = typeof(Type); + _argumentType = typeof(Type); - m_value = null; + _value = null; if (encodedArg.StringValue is not null) - m_value = ResolveType(scope, encodedArg.StringValue); + _value = ResolveType(scope, encodedArg.StringValue); } else if (encodedType == CustomAttributeEncoding.Array) { @@ -608,11 +593,11 @@ internal CustomAttributeTypedArgument(RuntimeModule scope, CustomAttributeEncode elementType = CustomAttributeEncodingToType(encodedType); } - m_argumentType = elementType.MakeArrayType(); + _argumentType = elementType.MakeArrayType(); if (encodedArg.ArrayValue is null) { - m_value = null; + _value = null; } else { @@ -620,13 +605,13 @@ internal CustomAttributeTypedArgument(RuntimeModule scope, CustomAttributeEncode for (int i = 0; i < arrayValue.Length; i++) arrayValue[i] = new CustomAttributeTypedArgument(scope, encodedArg.ArrayValue[i]); - m_value = Array.AsReadOnly(arrayValue); + _value = Array.AsReadOnly(arrayValue); } } else { - m_argumentType = CustomAttributeEncodingToType(encodedType); - m_value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedType); + _argumentType = CustomAttributeEncodingToType(encodedType); + _value = EncodedValueToRawValue(encodedArg.PrimitiveValue, encodedType); } } } @@ -1168,7 +1153,7 @@ private static void AddCustomAttributes( // The derivedAttributes list must be passed by value so that it is not modified with the discovered attributes RuntimeType.ListBuilder derivedAttributes) { - CustomAttributeRecord[] car = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken); + CustomAttributeRecord[] car = RuntimeCustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedMetadataToken); if (attributeFilterType is null && car.Length == 0) { @@ -1435,7 +1420,7 @@ internal static AttributeUsageAttribute GetAttributeUsage(RuntimeType decoratedA { RuntimeModule decoratedModule = decoratedAttribute.GetRuntimeModule(); MetadataImport scope = decoratedModule.MetadataImport; - CustomAttributeRecord[] car = CustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedAttribute.MetadataToken); + CustomAttributeRecord[] car = RuntimeCustomAttributeData.GetCustomAttributeRecords(decoratedModule, decoratedAttribute.MetadataToken); AttributeUsageAttribute? attributeUsageAttribute = null; diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs index 17721025b51eaf..75404eaaae1202 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs @@ -104,7 +104,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } #endregion diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs index 85625ac3150a98..193c22d849d531 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs @@ -89,7 +89,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } #endregion diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs index f66ab6dedbb581..6e3f0b0122a8fd 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs @@ -271,7 +271,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } #endregion diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs index 0b31bf27759e29..8f65ca6a66c961 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs @@ -425,7 +425,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } #endregion diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs index 77b2d5e8f5a7a3..0bbfa609ded974 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs @@ -320,8 +320,8 @@ public override bool HasDefaultValue if (raw) { CustomAttributeTypedArgument value = - CustomAttributeData.Filter( - CustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0); + RuntimeCustomAttributeData.Filter( + RuntimeCustomAttributeData.GetCustomAttributes(this), typeof(DateTimeConstantAttribute), 0); if (value.ArgumentType != null) return new DateTime((long)value.Value!); @@ -541,7 +541,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } #endregion } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs index 2eb46bf49b96f4..5a2440f10590f6 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs @@ -165,7 +165,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } #endregion diff --git a/src/coreclr/vm/corelib.h b/src/coreclr/vm/corelib.h index 3b5ac38321827e..0de5ce80229ee1 100644 --- a/src/coreclr/vm/corelib.h +++ b/src/coreclr/vm/corelib.h @@ -257,11 +257,11 @@ DEFINE_FIELD_U(m_type, CustomAttributeValue, m_type) DEFINE_CLASS(CUSTOM_ATTRIBUTE_ENCODED_ARGUMENT, Reflection, CustomAttributeEncodedArgument) DEFINE_CLASS_U(Reflection, CustomAttributeNamedParameter, CustomAttributeNamedArgument) -DEFINE_FIELD_U(m_argumentName, CustomAttributeNamedArgument, m_argumentName) -DEFINE_FIELD_U(m_fieldOrProperty, CustomAttributeNamedArgument, m_propertyOrField) -DEFINE_FIELD_U(m_padding, CustomAttributeNamedArgument, m_padding) -DEFINE_FIELD_U(m_type, CustomAttributeNamedArgument, m_type) -DEFINE_FIELD_U(m_encodedArgument, CustomAttributeNamedArgument, m_value) +DEFINE_FIELD_U(_argumentName, CustomAttributeNamedArgument, m_argumentName) +DEFINE_FIELD_U(_fieldOrProperty, CustomAttributeNamedArgument, m_propertyOrField) +DEFINE_FIELD_U(_padding, CustomAttributeNamedArgument, m_padding) +DEFINE_FIELD_U(_type, CustomAttributeNamedArgument, m_type) +DEFINE_FIELD_U(_encodedArgument, CustomAttributeNamedArgument, m_value) DEFINE_CLASS_U(Reflection, CustomAttributeCtorParameter, CustomAttributeArgument) DEFINE_FIELD_U(m_type, CustomAttributeArgument, m_type) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs index b704dac05496fb..d5011b5886cab1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs @@ -6,7 +6,7 @@ namespace System.Reflection { - public partial class CustomAttributeData + public class CustomAttributeData { #region Public Static Members public static IList GetCustomAttributes(MemberInfo target) @@ -42,6 +42,10 @@ public static IList GetCustomAttributes(ParameterInfo targe } #endregion + protected CustomAttributeData() + { + } + #region Object Override public override string ToString() { @@ -51,22 +55,26 @@ public override string ToString() vsb.Append(Constructor.DeclaringType!.FullName); vsb.Append('('); - bool first = true; - - int count = ConstructorArguments.Count; + IList constructorArguments = ConstructorArguments; + int count = constructorArguments.Count; for (int i = 0; i < count; i++) { - if (!first) vsb.Append(", "); - vsb.Append(ConstructorArguments[i].ToString()); - first = false; + if (i != 0) + { + vsb.Append(", "); + } + vsb.Append(constructorArguments[i].ToString()); } - count = NamedArguments.Count; + IList namedArguments = NamedArguments; + count = namedArguments.Count; for (int i = 0; i < count; i++) { - if (!first) vsb.Append(", "); - vsb.Append(NamedArguments[i].ToString()); - first = false; + if (i != 0) + { + vsb.Append(", "); + } + vsb.Append(namedArguments[i].ToString()); } vsb.Append(")]"); @@ -79,6 +87,11 @@ public override string ToString() #region Public Members public virtual Type AttributeType => Constructor.DeclaringType!; + + // Expected to be overriden + public virtual ConstructorInfo Constructor => null!; + public virtual IList ConstructorArguments => null!; + public virtual IList NamedArguments => null!; #endregion } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs index dd31ae35599762..385b9b6b63384f 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs @@ -8,8 +8,8 @@ public readonly partial struct CustomAttributeNamedArgument public static bool operator ==(CustomAttributeNamedArgument left, CustomAttributeNamedArgument right) => left.Equals(right); public static bool operator !=(CustomAttributeNamedArgument left, CustomAttributeNamedArgument right) => !left.Equals(right); - private readonly MemberInfo m_memberInfo; - private readonly CustomAttributeTypedArgument m_value; + private readonly MemberInfo _memberInfo; + private readonly CustomAttributeTypedArgument _value; public CustomAttributeNamedArgument(MemberInfo memberInfo, object? value) { @@ -30,19 +30,19 @@ public CustomAttributeNamedArgument(MemberInfo memberInfo, object? value) throw new ArgumentException(SR.Argument_InvalidMemberForNamedArgument); } - m_memberInfo = memberInfo; - m_value = new CustomAttributeTypedArgument(type, value); + _memberInfo = memberInfo; + _value = new CustomAttributeTypedArgument(type, value); } public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedArgument typedArgument) { - m_memberInfo = memberInfo ?? throw new ArgumentNullException(nameof(memberInfo)); - m_value = typedArgument; + _memberInfo = memberInfo ?? throw new ArgumentNullException(nameof(memberInfo)); + _value = typedArgument; } public override string ToString() { - if (m_memberInfo == null) + if (_memberInfo == null) return base.ToString()!; return $"{MemberInfo.Name} = {TypedValue.ToString(ArgumentType != typeof(object))}"; @@ -59,12 +59,12 @@ public override bool Equals(object? obj) } internal Type ArgumentType => - m_memberInfo is FieldInfo ? - ((FieldInfo)m_memberInfo).FieldType : - ((PropertyInfo)m_memberInfo).PropertyType; + _memberInfo is FieldInfo ? + ((FieldInfo)_memberInfo).FieldType : + ((PropertyInfo)_memberInfo).PropertyType; - public MemberInfo MemberInfo => m_memberInfo; - public CustomAttributeTypedArgument TypedValue => m_value; + public MemberInfo MemberInfo => _memberInfo; + public CustomAttributeTypedArgument TypedValue => _value; public string MemberName => MemberInfo.Name; public bool IsField => MemberInfo is FieldInfo; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs index 46fb5dd5c68a3c..034b4b481ea346 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs @@ -9,11 +9,10 @@ namespace System.Reflection public readonly partial struct CustomAttributeTypedArgument { public static bool operator ==(CustomAttributeTypedArgument left, CustomAttributeTypedArgument right) => left.Equals(right); - public static bool operator !=(CustomAttributeTypedArgument left, CustomAttributeTypedArgument right) => !left.Equals(right); - private readonly object? m_value; - private readonly Type m_argumentType; + private readonly object? _value; + private readonly Type _argumentType; public CustomAttributeTypedArgument(Type argumentType, object? value) { @@ -21,8 +20,8 @@ public CustomAttributeTypedArgument(Type argumentType, object? value) if (argumentType == null) throw new ArgumentNullException(nameof(argumentType)); - m_value = (value is null) ? null : CanonicalizeValue(value); - m_argumentType = argumentType; + _value = (value is null) ? null : CanonicalizeValue(value); + _argumentType = argumentType; } public CustomAttributeTypedArgument(object value) @@ -31,8 +30,8 @@ public CustomAttributeTypedArgument(object value) if (value == null) throw new ArgumentNullException(nameof(value)); - m_value = CanonicalizeValue(value); - m_argumentType = value.GetType(); + _value = CanonicalizeValue(value); + _argumentType = value.GetType(); } @@ -40,7 +39,7 @@ public CustomAttributeTypedArgument(object value) internal string ToString(bool typed) { - if (m_argumentType == null) + if (_argumentType == null) return base.ToString()!; if (ArgumentType.IsEnum) @@ -67,10 +66,11 @@ internal string ToString(bool typed) result.Append("new "); result.Append(elementType.IsEnum ? elementType.FullName : elementType.Name); result.Append('['); - result.Append(array.Count.ToString()); + int count = array.Count; + result.Append(count.ToString()); result.Append(']'); - for (int i = 0; i < array.Count; i++) + for (int i = 0; i < count; i++) { if (i != 0) { @@ -90,7 +90,15 @@ internal string ToString(bool typed) public override int GetHashCode() => base.GetHashCode(); public override bool Equals(object? obj) => obj == (object)this; - public Type ArgumentType => m_argumentType; - public object? Value => m_value; + public Type ArgumentType => _argumentType; + public object? Value => _value; + + private static object CanonicalizeValue(object value) + { + if (value.GetType().IsEnum) + return ((Enum)value).GetValue(); + + return value; + } } } diff --git a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs index 307d16a0383553..1eadda787d6453 100644 --- a/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs +++ b/src/libraries/System.Private.CoreLib/src/System/RuntimeType.cs @@ -68,7 +68,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } // GetDefaultMembers diff --git a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj index 7d9be765d169f7..de44fd1c3541b1 100644 --- a/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/mono/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -195,12 +195,11 @@ - - + diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs index 52ef9454f0078e..17b3970929ab88 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs @@ -544,9 +544,9 @@ internal static IList GetCustomAttributesDataBase(ICustomAt count = 0; if ((Attributes & TypeAttributes.Serializable) != 0) - attrsData[count++] = new CustomAttributeData((typeof(SerializableAttribute)).GetConstructor(Type.EmptyTypes)!); + attrsData[count++] = new RuntimeCustomAttributeData((typeof(SerializableAttribute)).GetConstructor(Type.EmptyTypes)!); if ((Attributes & TypeAttributes.Import) != 0) - attrsData[count++] = new CustomAttributeData((typeof(ComImportAttribute)).GetConstructor(Type.EmptyTypes)!); + attrsData[count++] = new RuntimeCustomAttributeData((typeof(ComImportAttribute)).GetConstructor(Type.EmptyTypes)!); return attrsData; } diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.Mono.cs deleted file mode 100644 index b8ce8994fa52ab..00000000000000 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.Mono.cs +++ /dev/null @@ -1,16 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -namespace System.Reflection -{ - public partial struct CustomAttributeTypedArgument - { - private static object CanonicalizeValue(object value) - { - if (value.GetType().IsEnum) - return ((Enum)value).GetValue(); - - return value; - } - } -} diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/FieldInfo.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/FieldInfo.Mono.cs index ea843ea40893ac..537b06c37d1406 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/FieldInfo.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/FieldInfo.Mono.cs @@ -85,11 +85,11 @@ internal virtual int GetFieldOffset() count = 0; if (IsNotSerialized) - attrsData[count++] = new CustomAttributeData((typeof(NonSerializedAttribute)).GetConstructor(Type.EmptyTypes)!); + attrsData[count++] = new RuntimeCustomAttributeData((typeof(NonSerializedAttribute)).GetConstructor(Type.EmptyTypes)!); if (DeclaringType.IsExplicitLayout) { var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(typeof(int), GetFieldOffset()) }; - attrsData[count++] = new CustomAttributeData( + attrsData[count++] = new RuntimeCustomAttributeData( (typeof(FieldOffsetAttribute)).GetConstructor(new[] { typeof(int) })!, ctorArgs, Array.Empty()); @@ -98,7 +98,7 @@ internal virtual int GetFieldOffset() if (marshalAs != null) { var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(typeof(UnmanagedType), marshalAs.Value) }; - attrsData[count++] = new CustomAttributeData( + attrsData[count++] = new RuntimeCustomAttributeData( (typeof(MarshalAsAttribute)).GetConstructor(new[] { typeof(UnmanagedType) })!, ctorArgs, Array.Empty());//FIXME Get named params diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs index fdcf45fac64f93..d5fa9e4e31d83b 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs @@ -275,7 +275,7 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } public override object[] GetCustomAttributes(bool inherit) diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs similarity index 92% rename from src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.Mono.cs rename to src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs index e3d393670cf970..c635e7ef28c0a9 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs @@ -34,7 +34,7 @@ namespace System.Reflection { - public partial class CustomAttributeData + internal sealed class RuntimeCustomAttributeData : CustomAttributeData { private sealed class LazyCAttrData { @@ -48,12 +48,8 @@ private sealed class LazyCAttrData private IList namedArgs = null!; private LazyCAttrData? lazyData; - protected CustomAttributeData() - { - } - // custom-attrs.c:create_custom_attr_data () - internal CustomAttributeData(ConstructorInfo ctorInfo, Assembly assembly, IntPtr data, uint data_length) + internal RuntimeCustomAttributeData(ConstructorInfo ctorInfo, Assembly assembly, IntPtr data, uint data_length) { this.ctorInfo = ctorInfo; this.lazyData = new LazyCAttrData(); @@ -62,12 +58,12 @@ internal CustomAttributeData(ConstructorInfo ctorInfo, Assembly assembly, IntPtr this.lazyData.data_length = data_length; } - internal CustomAttributeData(ConstructorInfo ctorInfo) + internal RuntimeCustomAttributeData(ConstructorInfo ctorInfo) : this(ctorInfo, Array.Empty(), Array.Empty()) { } - internal CustomAttributeData(ConstructorInfo ctorInfo, IList ctorArgs, IList namedArgs) + internal RuntimeCustomAttributeData(ConstructorInfo ctorInfo, IList ctorArgs, IList namedArgs) { this.ctorInfo = ctorInfo; this.ctorArgs = ctorArgs; @@ -94,7 +90,7 @@ private void ResolveArguments() } public - virtual + override ConstructorInfo Constructor { get @@ -104,7 +100,7 @@ ConstructorInfo Constructor } public - virtual + override IList ConstructorArguments { get @@ -115,7 +111,7 @@ IList ConstructorArguments } public - virtual + override IList NamedArguments { get diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs index 4ba3798d241612..a9844f7e1818af 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeEventInfo.cs @@ -201,7 +201,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } public override int MetadataToken diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs index 06eb9ce0395641..a156178708443b 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeFieldInfo.cs @@ -269,7 +269,7 @@ internal RuntimeFieldInfo Clone(string newName) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } private void CheckGeneric() diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs index b642d0903639a0..fc5cf2509b889e 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs @@ -595,7 +595,7 @@ private Attribute GetDllImportAttribute() count = 0; if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0) - attrsData[count++] = new CustomAttributeData((typeof(PreserveSigAttribute)).GetConstructor(Type.EmptyTypes)!); + attrsData[count++] = new RuntimeCustomAttributeData((typeof(PreserveSigAttribute)).GetConstructor(Type.EmptyTypes)!); if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) attrsData[count++] = GetDllImportAttributeData()!; @@ -657,7 +657,7 @@ private Attribute GetDllImportAttribute() new CustomAttributeNamedArgument (attrType.GetField ("ThrowOnUnmappableChar")!, throwOnUnmappableChar) }; - return new CustomAttributeData( + return new RuntimeCustomAttributeData( attrType.GetConstructor(new[] { typeof(string) })!, ctorArgs, namedArgs); @@ -750,7 +750,7 @@ public override MethodBody GetMethodBody() public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore(other); @@ -988,7 +988,7 @@ public override string ToString() public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore(other); diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs index a07856f346e0ac..692d7ff29926c9 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeModule.cs @@ -341,7 +341,7 @@ Type[] GetTypes() public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } internal RuntimeAssembly GetRuntimeAssembly() diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs index ecf4b3bf325c9a..43cc6a6245429e 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs @@ -206,7 +206,7 @@ bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } [MethodImplAttribute(MethodImplOptions.InternalCall)] @@ -266,15 +266,15 @@ public override IList GetCustomAttributesData() count = 0; if (IsIn) - attrsData[count++] = new CustomAttributeData((typeof(InAttribute)).GetConstructor(Type.EmptyTypes)!); + attrsData[count++] = new RuntimeCustomAttributeData((typeof(InAttribute)).GetConstructor(Type.EmptyTypes)!); if (IsOut) - attrsData[count++] = new CustomAttributeData((typeof(OutAttribute)).GetConstructor(Type.EmptyTypes)!); + attrsData[count++] = new RuntimeCustomAttributeData((typeof(OutAttribute)).GetConstructor(Type.EmptyTypes)!); if (IsOptional) - attrsData[count++] = new CustomAttributeData((typeof(OptionalAttribute)).GetConstructor(Type.EmptyTypes)!); + attrsData[count++] = new RuntimeCustomAttributeData((typeof(OptionalAttribute)).GetConstructor(Type.EmptyTypes)!); if (marshalAs != null) { var ctorArgs = new CustomAttributeTypedArgument[] { new CustomAttributeTypedArgument(typeof(UnmanagedType), marshalAs.Value) }; - attrsData[count++] = new CustomAttributeData( + attrsData[count++] = new RuntimeCustomAttributeData( (typeof(MarshalAsAttribute)).GetConstructor(new[] { typeof(UnmanagedType) })!, ctorArgs, Array.Empty());//FIXME Get named params diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs index 32d4645a674539..0da72f4569f01e 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/RuntimePropertyInfo.cs @@ -480,7 +480,7 @@ public override void SetValue(object? obj, object? value, BindingFlags invokeAtt public override IList GetCustomAttributesData() { - return CustomAttributeData.GetCustomAttributesInternal(this); + return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore(other); diff --git a/src/mono/mono/metadata/custom-attrs.c b/src/mono/mono/metadata/custom-attrs.c index ac72222d2366ca..48571da6a085ea 100644 --- a/src/mono/mono/metadata/custom-attrs.c +++ b/src/mono/mono/metadata/custom-attrs.c @@ -48,7 +48,7 @@ static gboolean type_is_reference (MonoType *type); static GENERATE_GET_CLASS_WITH_CACHE (custom_attribute_typed_argument, "System.Reflection", "CustomAttributeTypedArgument"); static GENERATE_GET_CLASS_WITH_CACHE (custom_attribute_named_argument, "System.Reflection", "CustomAttributeNamedArgument"); -static GENERATE_TRY_GET_CLASS_WITH_CACHE (customattribute_data, "System.Reflection", "CustomAttributeData"); +static GENERATE_TRY_GET_CLASS_WITH_CACHE (customattribute_data, "System.Reflection", "RuntimeCustomAttributeData"); static MonoCustomAttrInfo* mono_custom_attrs_from_builders_handle (MonoImage *alloc_img, MonoImage *image, MonoArrayHandle cattrs); @@ -1350,7 +1350,7 @@ mono_reflection_create_custom_attr_data_args_noalloc (MonoImage *image, MonoMeth } void -ves_icall_System_Reflection_CustomAttributeData_ResolveArgumentsInternal (MonoReflectionMethodHandle ref_method_h, MonoReflectionAssemblyHandle assembly_h, +ves_icall_System_Reflection_RuntimeCustomAttributeData_ResolveArgumentsInternal (MonoReflectionMethodHandle ref_method_h, MonoReflectionAssemblyHandle assembly_h, gpointer data, guint32 len, MonoArrayHandleOut ctor_args_h, MonoArrayHandleOut named_args_h, MonoError *error) @@ -1441,7 +1441,7 @@ try_get_cattr_data_class (MonoError* error) error_init (error); MonoClass *res = mono_class_try_get_customattribute_data_class (); if (!res) - mono_error_set_execution_engine (error, "Class System.Reflection.CustomAttributeData not found, probably removed by the linker"); + mono_error_set_execution_engine (error, "Class System.Reflection.RuntimeCustomAttributeData not found, probably removed by the linker"); return res; } diff --git a/src/mono/mono/metadata/icall-def-netcore.h b/src/mono/mono/metadata/icall-def-netcore.h index e1fec10c36672d..5937005e0064dd 100644 --- a/src/mono/mono/metadata/icall-def-netcore.h +++ b/src/mono/mono/metadata/icall-def-netcore.h @@ -207,8 +207,8 @@ HANDLES(MCATTR_1, "GetCustomAttributesDataInternal", ves_icall_MonoCustomAttrs_G HANDLES(MCATTR_2, "GetCustomAttributesInternal", ves_icall_MonoCustomAttrs_GetCustomAttributesInternal, MonoArray, 3, (MonoObject, MonoReflectionType, MonoBoolean)) HANDLES(MCATTR_3, "IsDefinedInternal", ves_icall_MonoCustomAttrs_IsDefinedInternal, MonoBoolean, 2, (MonoObject, MonoReflectionType)) -ICALL_TYPE(CATTR_DATA, "System.Reflection.CustomAttributeData", CATTR_DATA_1) -HANDLES(CATTR_DATA_1, "ResolveArgumentsInternal", ves_icall_System_Reflection_CustomAttributeData_ResolveArgumentsInternal, void, 6, (MonoReflectionMethod, MonoReflectionAssembly, gpointer, guint32, MonoArrayOut, MonoArrayOut)) +ICALL_TYPE(CATTR_DATA, "System.Reflection.RuntimeCustomAttributeData", CATTR_DATA_1) +HANDLES(CATTR_DATA_1, "ResolveArgumentsInternal", ves_icall_System_Reflection_RuntimeCustomAttributeData_ResolveArgumentsInternal, void, 6, (MonoReflectionMethod, MonoReflectionAssembly, gpointer, guint32, MonoArrayOut, MonoArrayOut)) ICALL_TYPE(ASSEMB, "System.Reflection.Emit.AssemblyBuilder", ASSEMB_1) HANDLES(ASSEMB_1, "UpdateNativeCustomAttributes", ves_icall_AssemblyBuilder_UpdateNativeCustomAttributes, void, 1, (MonoReflectionAssemblyBuilder)) From 6d6274ede2ec3b5285b8cadf363592e3fa89208e Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 08:10:10 -0700 Subject: [PATCH 02/11] Fix linker build break --- .../src/System/Reflection/CustomAttribute.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs b/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs index 17b3970929ab88..24d588afba9bb9 100644 --- a/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs +++ b/src/mono/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs @@ -309,7 +309,7 @@ internal static object[] GetCustomAttributes(ICustomAttributeProvider obj, bool } [MethodImplAttribute(MethodImplOptions.InternalCall)] - [DynamicDependency("#ctor(System.Reflection.ConstructorInfo,System.Reflection.Assembly,System.IntPtr,System.UInt32)", typeof(CustomAttributeData))] + [DynamicDependency("#ctor(System.Reflection.ConstructorInfo,System.Reflection.Assembly,System.IntPtr,System.UInt32)", typeof(RuntimeCustomAttributeData))] [DynamicDependency("#ctor(System.Reflection.MemberInfo,System.Object)", typeof(CustomAttributeNamedArgument))] [DynamicDependency("#ctor(System.Type,System.Object)", typeof(CustomAttributeTypedArgument))] private static extern CustomAttributeData[] GetCustomAttributesDataInternal(ICustomAttributeProvider obj); From 00aa646498c645be3bae93611b979134401c9f70 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 08:29:29 -0700 Subject: [PATCH 03/11] Simplify CanonicalizeValue --- .../Reflection/CustomAttributeTypedArgument.cs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs index 034b4b481ea346..7ec8d5432e8290 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs @@ -16,17 +16,15 @@ public readonly partial struct CustomAttributeTypedArgument public CustomAttributeTypedArgument(Type argumentType, object? value) { - // value can be null. if (argumentType == null) throw new ArgumentNullException(nameof(argumentType)); - _value = (value is null) ? null : CanonicalizeValue(value); + _value = CanonicalizeValue(value); _argumentType = argumentType; } public CustomAttributeTypedArgument(object value) { - // value cannot be null. if (value == null) throw new ArgumentNullException(nameof(value)); @@ -93,12 +91,6 @@ internal string ToString(bool typed) public Type ArgumentType => _argumentType; public object? Value => _value; - private static object CanonicalizeValue(object value) - { - if (value.GetType().IsEnum) - return ((Enum)value).GetValue(); - - return value; - } + private static object? CanonicalizeValue(object? value) => (value is Enum e) ? e.GetValue() : value; } } From 10890e088cb08490034e8518d5817f44adccaf32 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 08:37:26 -0700 Subject: [PATCH 04/11] Use pattern matching --- .../Reflection/CustomAttributeNamedArgument.cs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs index 385b9b6b63384f..8a1a21bde82d66 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs @@ -16,19 +16,12 @@ public CustomAttributeNamedArgument(MemberInfo memberInfo, object? value) if (memberInfo == null) throw new ArgumentNullException(nameof(memberInfo)); - Type type; - if (memberInfo is FieldInfo field) + Type type = memberInfo switch { - type = field.FieldType; - } - else if (memberInfo is PropertyInfo property) - { - type = property.PropertyType; - } - else - { - throw new ArgumentException(SR.Argument_InvalidMemberForNamedArgument); - } + FieldInfo field => field.FieldType, + PropertyInfo property => property.PropertyType, + _ => throw new ArgumentException(SR.Argument_InvalidMemberForNamedArgument) + }; _memberInfo = memberInfo; _value = new CustomAttributeTypedArgument(type, value); From 24ba08c83b844b9b2751dde07b6ce6ffaf9a2f9d Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 09:20:30 -0700 Subject: [PATCH 05/11] Update src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs Co-authored-by: Marek Safar --- .../src/System/Reflection/CustomAttributeNamedArgument.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs index 8a1a21bde82d66..90fd13aba8cf18 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs @@ -52,7 +52,9 @@ public override bool Equals(object? obj) } internal Type ArgumentType => - _memberInfo is FieldInfo ? + _memberInfo is FieldInfo fi ? + fi.FieldType : + ((PropertyInfo)_memberInfo).PropertyType; ((FieldInfo)_memberInfo).FieldType : ((PropertyInfo)_memberInfo).PropertyType; From 9a3841d088eca1266c5cc51e083dffd44588fd87 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 09:22:42 -0700 Subject: [PATCH 06/11] Feedback --- .../src/System/Reflection/RuntimeCustomAttributeData.cs | 2 +- .../src/System/Reflection/CustomAttributeNamedArgument.cs | 4 ++-- .../src/System/Reflection/CustomAttributeTypedArgument.cs | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs index 6a90eb2ca7f960..4bdc413dfc2f49 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs @@ -12,7 +12,7 @@ namespace System.Reflection { - public class RuntimeCustomAttributeData : CustomAttributeData + internal sealed class RuntimeCustomAttributeData : CustomAttributeData { #region Internal Static Members internal static IList GetCustomAttributesInternal(RuntimeType target) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs index 8a1a21bde82d66..b8235f26ae7bf4 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs @@ -13,7 +13,7 @@ public readonly partial struct CustomAttributeNamedArgument public CustomAttributeNamedArgument(MemberInfo memberInfo, object? value) { - if (memberInfo == null) + if (memberInfo is null) throw new ArgumentNullException(nameof(memberInfo)); Type type = memberInfo switch @@ -35,7 +35,7 @@ public CustomAttributeNamedArgument(MemberInfo memberInfo, CustomAttributeTypedA public override string ToString() { - if (_memberInfo == null) + if (_memberInfo is null) return base.ToString()!; return $"{MemberInfo.Name} = {TypedValue.ToString(ArgumentType != typeof(object))}"; diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs index 7ec8d5432e8290..18d033b5c4394d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs @@ -16,7 +16,7 @@ public readonly partial struct CustomAttributeTypedArgument public CustomAttributeTypedArgument(Type argumentType, object? value) { - if (argumentType == null) + if (argumentType is null) throw new ArgumentNullException(nameof(argumentType)); _value = CanonicalizeValue(value); @@ -25,7 +25,7 @@ public CustomAttributeTypedArgument(Type argumentType, object? value) public CustomAttributeTypedArgument(object value) { - if (value == null) + if (value is null) throw new ArgumentNullException(nameof(value)); _value = CanonicalizeValue(value); @@ -37,13 +37,13 @@ public CustomAttributeTypedArgument(object value) internal string ToString(bool typed) { - if (_argumentType == null) + if (_argumentType is null) return base.ToString()!; if (ArgumentType.IsEnum) return typed ? $"{Value}" : $"({ArgumentType.FullName}){Value}"; - if (Value == null) + if (Value is null) return typed ? "null" : $"({ArgumentType.Name})null"; if (ArgumentType == typeof(string)) From 26f880ade54b85bd10c13016ebb62a64fefe4c64 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 09:26:28 -0700 Subject: [PATCH 07/11] Fix bad merge --- .../src/System/Reflection/CustomAttributeNamedArgument.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs index 176a70e12db82a..9699e14e5d1e53 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs @@ -55,8 +55,6 @@ public override bool Equals(object? obj) _memberInfo is FieldInfo fi ? fi.FieldType : ((PropertyInfo)_memberInfo).PropertyType; - ((FieldInfo)_memberInfo).FieldType : - ((PropertyInfo)_memberInfo).PropertyType; public MemberInfo MemberInfo => _memberInfo; public CustomAttributeTypedArgument TypedValue => _value; From 2275e34371cbbb223d470b9e27223895aeb6b85e Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 10:28:17 -0700 Subject: [PATCH 08/11] Revert incorrect change --- src/coreclr/vm/corelib.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/coreclr/vm/corelib.h b/src/coreclr/vm/corelib.h index 0de5ce80229ee1..3b5ac38321827e 100644 --- a/src/coreclr/vm/corelib.h +++ b/src/coreclr/vm/corelib.h @@ -257,11 +257,11 @@ DEFINE_FIELD_U(m_type, CustomAttributeValue, m_type) DEFINE_CLASS(CUSTOM_ATTRIBUTE_ENCODED_ARGUMENT, Reflection, CustomAttributeEncodedArgument) DEFINE_CLASS_U(Reflection, CustomAttributeNamedParameter, CustomAttributeNamedArgument) -DEFINE_FIELD_U(_argumentName, CustomAttributeNamedArgument, m_argumentName) -DEFINE_FIELD_U(_fieldOrProperty, CustomAttributeNamedArgument, m_propertyOrField) -DEFINE_FIELD_U(_padding, CustomAttributeNamedArgument, m_padding) -DEFINE_FIELD_U(_type, CustomAttributeNamedArgument, m_type) -DEFINE_FIELD_U(_encodedArgument, CustomAttributeNamedArgument, m_value) +DEFINE_FIELD_U(m_argumentName, CustomAttributeNamedArgument, m_argumentName) +DEFINE_FIELD_U(m_fieldOrProperty, CustomAttributeNamedArgument, m_propertyOrField) +DEFINE_FIELD_U(m_padding, CustomAttributeNamedArgument, m_padding) +DEFINE_FIELD_U(m_type, CustomAttributeNamedArgument, m_type) +DEFINE_FIELD_U(m_encodedArgument, CustomAttributeNamedArgument, m_value) DEFINE_CLASS_U(Reflection, CustomAttributeCtorParameter, CustomAttributeArgument) DEFINE_FIELD_U(m_type, CustomAttributeArgument, m_type) From 0bcb0f1e77e4dd7fe15ec85bc3cb2c7505c5a16e Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 12:01:59 -0700 Subject: [PATCH 09/11] Is this hitting codegen bug? --- .../src/System/Reflection/CustomAttributeData.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs index d5011b5886cab1..1794c1dcad5f9b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Generic; +using System.Runtime.CompilerServices; using System.Text; namespace System.Reflection @@ -47,6 +48,7 @@ protected CustomAttributeData() } #region Object Override + [MethodImpl(MethodImplOptions.NoOptimization)] // Workaround for codegen bug? public override string ToString() { var vsb = new ValueStringBuilder(stackalloc char[256]); From dcd0e382b06fc98781817fd1e6ffb6e93b3b8ac4 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 12:07:29 -0700 Subject: [PATCH 10/11] Fix Mono ICALL order --- src/mono/mono/metadata/icall-def-netcore.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mono/mono/metadata/icall-def-netcore.h b/src/mono/mono/metadata/icall-def-netcore.h index 5937005e0064dd..8e026693a109ee 100644 --- a/src/mono/mono/metadata/icall-def-netcore.h +++ b/src/mono/mono/metadata/icall-def-netcore.h @@ -207,9 +207,6 @@ HANDLES(MCATTR_1, "GetCustomAttributesDataInternal", ves_icall_MonoCustomAttrs_G HANDLES(MCATTR_2, "GetCustomAttributesInternal", ves_icall_MonoCustomAttrs_GetCustomAttributesInternal, MonoArray, 3, (MonoObject, MonoReflectionType, MonoBoolean)) HANDLES(MCATTR_3, "IsDefinedInternal", ves_icall_MonoCustomAttrs_IsDefinedInternal, MonoBoolean, 2, (MonoObject, MonoReflectionType)) -ICALL_TYPE(CATTR_DATA, "System.Reflection.RuntimeCustomAttributeData", CATTR_DATA_1) -HANDLES(CATTR_DATA_1, "ResolveArgumentsInternal", ves_icall_System_Reflection_RuntimeCustomAttributeData_ResolveArgumentsInternal, void, 6, (MonoReflectionMethod, MonoReflectionAssembly, gpointer, guint32, MonoArrayOut, MonoArrayOut)) - ICALL_TYPE(ASSEMB, "System.Reflection.Emit.AssemblyBuilder", ASSEMB_1) HANDLES(ASSEMB_1, "UpdateNativeCustomAttributes", ves_icall_AssemblyBuilder_UpdateNativeCustomAttributes, void, 1, (MonoReflectionAssemblyBuilder)) HANDLES(ASSEMB_2, "basic_init", ves_icall_AssemblyBuilder_basic_init, void, 1, (MonoReflectionAssemblyBuilder)) @@ -279,6 +276,9 @@ HANDLES(MCMETH_1, "GetGenericMethodDefinition_impl", ves_icall_RuntimeMethodInfo HANDLES(MCMETH_2, "InternalInvoke", ves_icall_InternalInvoke, MonoObject, 4, (MonoReflectionMethod, MonoObject, MonoArray, MonoExceptionOut)) HANDLES_REUSE_WRAPPER(MCMETH_4, "get_metadata_token", ves_icall_reflection_get_token) +ICALL_TYPE(CATTR_DATA, "System.Reflection.RuntimeCustomAttributeData", CATTR_DATA_1) +HANDLES(CATTR_DATA_1, "ResolveArgumentsInternal", ves_icall_System_Reflection_RuntimeCustomAttributeData_ResolveArgumentsInternal, void, 6, (MonoReflectionMethod, MonoReflectionAssembly, gpointer, guint32, MonoArrayOut, MonoArrayOut)) + ICALL_TYPE(MEV, "System.Reflection.RuntimeEventInfo", MEV_1) HANDLES(MEV_1, "get_event_info", ves_icall_RuntimeEventInfo_get_event_info, void, 2, (MonoReflectionMonoEvent, MonoEventInfo_ref)) HANDLES_REUSE_WRAPPER(MEV_2, "get_metadata_token", ves_icall_reflection_get_token) From e9b68dc37a39c665e0385daeb4bdc1e2eaf088b1 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 17 Jul 2021 13:28:31 -0700 Subject: [PATCH 11/11] Revert incorrect change --- .../System/Reflection/CustomAttributeData.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs index 1794c1dcad5f9b..69c9db7aba2c81 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs @@ -48,7 +48,6 @@ protected CustomAttributeData() } #region Object Override - [MethodImpl(MethodImplOptions.NoOptimization)] // Workaround for codegen bug? public override string ToString() { var vsb = new ValueStringBuilder(stackalloc char[256]); @@ -57,26 +56,24 @@ public override string ToString() vsb.Append(Constructor.DeclaringType!.FullName); vsb.Append('('); + bool first = true; + IList constructorArguments = ConstructorArguments; - int count = constructorArguments.Count; - for (int i = 0; i < count; i++) + int constructorArgumentsCount = constructorArguments.Count; + for (int i = 0; i < constructorArgumentsCount; i++) { - if (i != 0) - { - vsb.Append(", "); - } + if (!first) vsb.Append(", "); vsb.Append(constructorArguments[i].ToString()); + first = false; } IList namedArguments = NamedArguments; - count = namedArguments.Count; - for (int i = 0; i < count; i++) + int namedArgumentsCount = namedArguments.Count; + for (int i = 0; i < namedArgumentsCount; i++) { - if (i != 0) - { - vsb.Append(", "); - } + if (!first) vsb.Append(", "); vsb.Append(namedArguments[i].ToString()); + first = false; } vsb.Append(")]");