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..4bdc413dfc2f49 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 + internal sealed 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/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs index b704dac05496fb..69c9db7aba2c81 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeData.cs @@ -2,11 +2,12 @@ // 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 { - public partial class CustomAttributeData + public class CustomAttributeData { #region Public Static Members public static IList GetCustomAttributes(MemberInfo target) @@ -42,6 +43,10 @@ public static IList GetCustomAttributes(ParameterInfo targe } #endregion + protected CustomAttributeData() + { + } + #region Object Override public override string ToString() { @@ -53,19 +58,21 @@ public override string ToString() bool first = true; - int count = ConstructorArguments.Count; - for (int i = 0; i < count; i++) + IList constructorArguments = ConstructorArguments; + int constructorArgumentsCount = constructorArguments.Count; + for (int i = 0; i < constructorArgumentsCount; i++) { if (!first) vsb.Append(", "); - vsb.Append(ConstructorArguments[i].ToString()); + vsb.Append(constructorArguments[i].ToString()); first = false; } - count = NamedArguments.Count; - for (int i = 0; i < count; i++) + IList namedArguments = NamedArguments; + int namedArgumentsCount = namedArguments.Count; + for (int i = 0; i < namedArgumentsCount; i++) { if (!first) vsb.Append(", "); - vsb.Append(NamedArguments[i].ToString()); + vsb.Append(namedArguments[i].ToString()); first = false; } @@ -79,6 +86,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..9699e14e5d1e53 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeNamedArgument.cs @@ -8,41 +8,34 @@ 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) { - if (memberInfo == null) + if (memberInfo is 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) + }; - 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 is null) return base.ToString()!; return $"{MemberInfo.Name} = {TypedValue.ToString(ArgumentType != typeof(object))}"; @@ -59,12 +52,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 fi ? + fi.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..18d033b5c4394d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/CustomAttributeTypedArgument.cs @@ -9,30 +9,27 @@ 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) { - // value can be null. - if (argumentType == null) + if (argumentType is null) throw new ArgumentNullException(nameof(argumentType)); - m_value = (value is null) ? null : CanonicalizeValue(value); - m_argumentType = argumentType; + _value = CanonicalizeValue(value); + _argumentType = argumentType; } public CustomAttributeTypedArgument(object value) { - // value cannot be null. - if (value == null) + if (value is null) throw new ArgumentNullException(nameof(value)); - m_value = CanonicalizeValue(value); - m_argumentType = value.GetType(); + _value = CanonicalizeValue(value); + _argumentType = value.GetType(); } @@ -40,13 +37,13 @@ public CustomAttributeTypedArgument(object value) internal string ToString(bool typed) { - if (m_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)) @@ -67,10 +64,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 +88,9 @@ 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) => (value is Enum e) ? e.GetValue() : 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..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); @@ -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..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.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(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)