From 7535da5d207983ca94549f385eb2917788fabd0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Fri, 25 Sep 2026 14:57:17 +0900 Subject: [PATCH 1/2] Split RuntimeParameterInfo runtime-specific members Move the shared implementation to the libraries partition, keep CoreCLR-only members in a CoreCLR partial, and exclude the shared file from Mono. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: d3ab6114-67a0-44ee-b133-23b3f478be8a --- .../System.Private.CoreLib.csproj | 2 +- .../RuntimeParameterInfo.CoreCLR.cs | 186 ++++++++++++++++++ .../src/System.Private.CoreLib.csproj | 1 - .../System.Private.CoreLib.Shared.projitems | 1 + .../System/Reflection/RuntimeParameterInfo.cs | 177 ----------------- 5 files changed, 188 insertions(+), 179 deletions(-) create mode 100644 src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.CoreCLR.cs rename src/{coreclr => libraries}/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs (65%) diff --git a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj index 78141e32f0fa66..a024321cd793c7 100644 --- a/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/coreclr/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -194,7 +194,7 @@ - + diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.CoreCLR.cs new file mode 100644 index 00000000000000..193e628adcfe35 --- /dev/null +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.CoreCLR.cs @@ -0,0 +1,186 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Diagnostics; +using MdToken = System.Reflection.MetadataToken; + +namespace System.Reflection +{ + internal sealed partial class RuntimeParameterInfo : ParameterInfo + { + #region Private Data Members + private readonly int m_tkParamDef; + private readonly MetadataImport m_scope; + private readonly Signature? m_signature; + #endregion + + #region Static Members + internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo method, MemberInfo member, Signature sig) + { + Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo); + + return GetParameters(method, member, sig, out _, fetchReturnParameter: false); + } + + internal static ParameterInfo GetReturnParameter(IRuntimeMethodInfo method, MemberInfo member, Signature sig) + { + Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo); + + GetParameters(method, member, sig, out ParameterInfo? returnParameter, fetchReturnParameter: true); + return returnParameter!; + } + + private static ParameterInfo[] GetParameters( + IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, out ParameterInfo? returnParameter, bool fetchReturnParameter) + { + // The lifetime rules for MetadataImport expect these two objects to be the same instance. + // See the lifetime of MetadataImport, acquired through IRuntimeMethodInfo, but extended + // through the MemberInfo instance. + Debug.Assert(ReferenceEquals(methodHandle, member)); + + returnParameter = null; + int sigArgCount = sig.Arguments.Length; + ParameterInfo[] args = + fetchReturnParameter ? null! : + sigArgCount == 0 ? [] : + new ParameterInfo[sigArgCount]; + + int tkMethodDef = RuntimeMethodHandle.GetMethodDef(methodHandle); + int cParamDefs = 0; + + // Not all methods have tokens. Arrays, pointers and byRef types do not have tokens as they + // are generated on the fly by the runtime. + if (!MdToken.IsNullToken(tkMethodDef)) + { + MetadataImport scope = RuntimeMethodHandle.GetDeclaringType(methodHandle).GetRuntimeModule().MetadataImport; + + scope.EnumParams(tkMethodDef, out MetadataEnumResult tkParamDefs); + + cParamDefs = tkParamDefs.Length; + + // Not all parameters have tokens. Parameters may have no token + // if they have no name and no attributes. + if (cParamDefs > sigArgCount + 1 /* return type */) + throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch); + + for (int i = 0; i < cParamDefs; i++) + { + #region Populate ParameterInfos + int tkParamDef = tkParamDefs[i]; + + scope.GetParamDefProps(tkParamDef, out int position, out ParameterAttributes attr); + + position--; + + if (fetchReturnParameter && position == -1) + { + // more than one return parameter? + if (returnParameter != null) + throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch); + + returnParameter = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member); + } + else if (!fetchReturnParameter && position >= 0) + { + // position beyond sigArgCount? + if (position >= sigArgCount) + throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch); + + args[position] = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member); + } + #endregion + } + } + + // Fill in empty ParameterInfos for those without tokens + if (fetchReturnParameter) + { + returnParameter ??= new RuntimeParameterInfo(sig, default, 0, -1, (ParameterAttributes)0, member); + } + else + { + if (cParamDefs < args.Length + 1) + { + for (int i = 0; i < args.Length; i++) + { + if (args[i] != null) + continue; + + args[i] = new RuntimeParameterInfo(sig, default, 0, i, (ParameterAttributes)0, member); + } + } + } + + return args; + } + #endregion + + #region Constructor + private RuntimeParameterInfo( + Signature signature, MetadataImport scope, int tkParamDef, + int position, ParameterAttributes attributes, MemberInfo member) + { + Debug.Assert(member != null); + Debug.Assert(MdToken.IsNullToken(tkParamDef) == scope.Equals((MetadataImport)default)); + Debug.Assert(MdToken.IsNullToken(tkParamDef) || MdToken.IsTokenOfType(tkParamDef, MetadataTokenType.ParamDef)); + + PositionImpl = position; + MemberImpl = member; + m_signature = signature; + m_tkParamDef = MdToken.IsNullToken(tkParamDef) ? (int)MetadataTokenType.ParamDef : tkParamDef; + m_scope = scope; + AttrsImpl = attributes; + + ClassImpl = null; + NameImpl = null; + } + + // ctor for no metadata MethodInfo in the DynamicMethod and RuntimeMethodInfo cases + internal RuntimeParameterInfo(MethodInfo owner, string? name, Type parameterType, int position) + { + MemberImpl = owner; + NameImpl = name; + m_nameIsCached = true; + m_noMetadata = true; + ClassImpl = parameterType; + PositionImpl = position; + AttrsImpl = ParameterAttributes.None; + m_tkParamDef = (int)MetadataTokenType.ParamDef; + m_scope = default; + } + #endregion + + #region Public Methods + internal RuntimeModule? GetRuntimeModule() + { + RuntimeMethodInfo? method = Member as RuntimeMethodInfo; + RuntimeConstructorInfo? constructor = Member as RuntimeConstructorInfo; + RuntimePropertyInfo? property = Member as RuntimePropertyInfo; + + if (method != null) + return method.GetRuntimeModule(); + else if (constructor != null) + return constructor.GetRuntimeModule(); + else if (property != null) + return property.GetRuntimeModule(); + else + return null; + } + + public override int MetadataToken => m_tkParamDef; + + public override Type[] GetRequiredCustomModifiers() + { + return m_signature is null ? [] : m_signature.GetCustomModifiers(PositionImpl + 1, true); + } + + public override Type[] GetOptionalCustomModifiers() + { + return m_signature is null ? [] : m_signature.GetCustomModifiers(PositionImpl + 1, false); + } + + public override Type GetModifiedParameterType() => + ModifiedType.Create(unmodifiedType: ParameterType, m_signature, parameterIndex: PositionImpl + 1); + #endregion + } +} diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj index 8544928be4b471..bb342be277760a 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj @@ -63,7 +63,6 @@ - diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 867e3a6fdca01f..c0c997f4febaeb 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -807,6 +807,7 @@ + diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs similarity index 65% rename from src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs rename to src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs index 68717b8224f5ca..f65e1007c53d2d 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs @@ -16,115 +16,7 @@ namespace System.Reflection { internal sealed partial class RuntimeParameterInfo : ParameterInfo { - #region Static Members -#if !NATIVEAOT - internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo method, MemberInfo member, Signature sig) - { - Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo); - - return GetParameters(method, member, sig, out _, fetchReturnParameter: false); - } - - internal static ParameterInfo GetReturnParameter(IRuntimeMethodInfo method, MemberInfo member, Signature sig) - { - Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo); - - GetParameters(method, member, sig, out ParameterInfo? returnParameter, fetchReturnParameter: true); - return returnParameter!; - } - - private static ParameterInfo[] GetParameters( - IRuntimeMethodInfo methodHandle, MemberInfo member, Signature sig, out ParameterInfo? returnParameter, bool fetchReturnParameter) - { - // The lifetime rules for MetadataImport expect these two objects to be the same instance. - // See the lifetime of MetadataImport, acquired through IRuntimeMethodInfo, but extended - // through the MemberInfo instance. - Debug.Assert(ReferenceEquals(methodHandle, member)); - - returnParameter = null; - int sigArgCount = sig.Arguments.Length; - ParameterInfo[] args = - fetchReturnParameter ? null! : - sigArgCount == 0 ? [] : - new ParameterInfo[sigArgCount]; - - int tkMethodDef = RuntimeMethodHandle.GetMethodDef(methodHandle); - int cParamDefs = 0; - - // Not all methods have tokens. Arrays, pointers and byRef types do not have tokens as they - // are generated on the fly by the runtime. - if (!MdToken.IsNullToken(tkMethodDef)) - { - MetadataImport scope = RuntimeMethodHandle.GetDeclaringType(methodHandle).GetRuntimeModule().MetadataImport; - - scope.EnumParams(tkMethodDef, out MetadataEnumResult tkParamDefs); - - cParamDefs = tkParamDefs.Length; - - // Not all parameters have tokens. Parameters may have no token - // if they have no name and no attributes. - if (cParamDefs > sigArgCount + 1 /* return type */) - throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch); - - for (int i = 0; i < cParamDefs; i++) - { - #region Populate ParameterInfos - int tkParamDef = tkParamDefs[i]; - - scope.GetParamDefProps(tkParamDef, out int position, out ParameterAttributes attr); - - position--; - - if (fetchReturnParameter && position == -1) - { - // more than one return parameter? - if (returnParameter != null) - throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch); - - returnParameter = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member); - } - else if (!fetchReturnParameter && position >= 0) - { - // position beyond sigArgCount? - if (position >= sigArgCount) - throw new BadImageFormatException(SR.BadImageFormat_ParameterSignatureMismatch); - - args[position] = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member); - } - #endregion - } - } - - // Fill in empty ParameterInfos for those without tokens - if (fetchReturnParameter) - { - returnParameter ??= new RuntimeParameterInfo(sig, default, 0, -1, (ParameterAttributes)0, member); - } - else - { - if (cParamDefs < args.Length + 1) - { - for (int i = 0; i < args.Length; i++) - { - if (args[i] != null) - continue; - - args[i] = new RuntimeParameterInfo(sig, default, 0, i, (ParameterAttributes)0, member); - } - } - } - - return args; - } -#endif - #endregion - #region Private Data Members -#if !NATIVEAOT - private readonly int m_tkParamDef; - private readonly MetadataImport m_scope; - private readonly Signature? m_signature; -#endif private volatile bool m_nameIsCached; private readonly bool m_noMetadata; private bool m_noDefaultValue; @@ -195,42 +87,6 @@ private RuntimeParameterInfo(RuntimeParameterInfo accessor, MemberInfo member) m_scope = accessor.m_scope; } -#if !NATIVEAOT - private RuntimeParameterInfo( - Signature signature, MetadataImport scope, int tkParamDef, - int position, ParameterAttributes attributes, MemberInfo member) - { - Debug.Assert(member != null); - Debug.Assert(MdToken.IsNullToken(tkParamDef) == scope.Equals((MetadataImport)default)); - Debug.Assert(MdToken.IsNullToken(tkParamDef) || MdToken.IsTokenOfType(tkParamDef, MetadataTokenType.ParamDef)); - - PositionImpl = position; - MemberImpl = member; - m_signature = signature; - m_tkParamDef = MdToken.IsNullToken(tkParamDef) ? (int)MetadataTokenType.ParamDef : tkParamDef; - m_scope = scope; - AttrsImpl = attributes; - - ClassImpl = null; - NameImpl = null; - } -#endif - - // ctor for no metadata MethodInfo in the DynamicMethod and RuntimeMethodInfo cases - internal RuntimeParameterInfo(MethodInfo owner, string? name, Type parameterType, int position) - { - MemberImpl = owner; - NameImpl = name; - m_nameIsCached = true; - m_noMetadata = true; - ClassImpl = parameterType; - PositionImpl = position; - AttrsImpl = ParameterAttributes.None; -#if !NATIVEAOT - m_tkParamDef = (int)MetadataTokenType.ParamDef; -#endif - m_scope = default; - } #endregion #region Public Methods @@ -453,39 +309,6 @@ private static DateTime GetRawDateTimeConstant(CustomAttributeData attr) return DBNull.Value; } -#if !NATIVEAOT - internal RuntimeModule? GetRuntimeModule() - { - RuntimeMethodInfo? method = Member as RuntimeMethodInfo; - RuntimeConstructorInfo? constructor = Member as RuntimeConstructorInfo; - RuntimePropertyInfo? property = Member as RuntimePropertyInfo; - - if (method != null) - return method.GetRuntimeModule(); - else if (constructor != null) - return constructor.GetRuntimeModule(); - else if (property != null) - return property.GetRuntimeModule(); - else - return null; - } - - public override int MetadataToken => m_tkParamDef; - - public override Type[] GetRequiredCustomModifiers() - { - return m_signature is null ? [] : m_signature.GetCustomModifiers(PositionImpl + 1, true); - } - - public override Type[] GetOptionalCustomModifiers() - { - return m_signature is null ? [] : m_signature.GetCustomModifiers(PositionImpl + 1, false); - } - - public override Type GetModifiedParameterType() => - ModifiedType.Create(unmodifiedType: ParameterType, m_signature, parameterIndex: PositionImpl + 1); -#endif - #endregion #region ICustomAttributeProvider From 27cde8777bffeabd6ae698209fe7aa37cfc9dbbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Mon, 28 Sep 2026 13:20:05 +0900 Subject: [PATCH 2/2] Address RuntimeParameterInfo review feedback Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: e63d0062-78e6-45aa-9cbd-9351fe5797ca --- .../RuntimeParameterInfo.CoreCLR.cs | 48 ++++------ .../RuntimeParameterInfo.NativeAot.cs | 40 ++++---- .../System/Reflection/RuntimeParameterInfo.cs | 91 ++++++++----------- 3 files changed, 73 insertions(+), 106 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.CoreCLR.cs index 193e628adcfe35..b8930562e0b481 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.CoreCLR.cs @@ -8,13 +8,10 @@ namespace System.Reflection { internal sealed partial class RuntimeParameterInfo : ParameterInfo { - #region Private Data Members - private readonly int m_tkParamDef; - private readonly MetadataImport m_scope; - private readonly Signature? m_signature; - #endregion + private readonly int _tkParamDef; + private readonly MetadataImport _scope; + private readonly Signature? _signature; - #region Static Members internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo method, MemberInfo member, Signature sig) { Debug.Assert(method is RuntimeMethodInfo || method is RuntimeConstructorInfo); @@ -65,7 +62,6 @@ private static ParameterInfo[] GetParameters( for (int i = 0; i < cParamDefs; i++) { - #region Populate ParameterInfos int tkParamDef = tkParamDefs[i]; scope.GetParamDefProps(tkParamDef, out int position, out ParameterAttributes attr); @@ -88,7 +84,6 @@ private static ParameterInfo[] GetParameters( args[position] = new RuntimeParameterInfo(sig, scope, tkParamDef, position, attr, member); } - #endregion } } @@ -113,9 +108,7 @@ private static ParameterInfo[] GetParameters( return args; } - #endregion - #region Constructor private RuntimeParameterInfo( Signature signature, MetadataImport scope, int tkParamDef, int position, ParameterAttributes attributes, MemberInfo member) @@ -126,9 +119,9 @@ private RuntimeParameterInfo( PositionImpl = position; MemberImpl = member; - m_signature = signature; - m_tkParamDef = MdToken.IsNullToken(tkParamDef) ? (int)MetadataTokenType.ParamDef : tkParamDef; - m_scope = scope; + _signature = signature; + _tkParamDef = MdToken.IsNullToken(tkParamDef) ? (int)MetadataTokenType.ParamDef : tkParamDef; + _scope = scope; AttrsImpl = attributes; ClassImpl = null; @@ -140,47 +133,40 @@ internal RuntimeParameterInfo(MethodInfo owner, string? name, Type parameterType { MemberImpl = owner; NameImpl = name; - m_nameIsCached = true; - m_noMetadata = true; + _nameIsCached = true; + _noMetadata = true; ClassImpl = parameterType; PositionImpl = position; AttrsImpl = ParameterAttributes.None; - m_tkParamDef = (int)MetadataTokenType.ParamDef; - m_scope = default; + _tkParamDef = (int)MetadataTokenType.ParamDef; + _scope = default; } - #endregion - #region Public Methods internal RuntimeModule? GetRuntimeModule() { - RuntimeMethodInfo? method = Member as RuntimeMethodInfo; - RuntimeConstructorInfo? constructor = Member as RuntimeConstructorInfo; - RuntimePropertyInfo? property = Member as RuntimePropertyInfo; - - if (method != null) + if (Member is RuntimeMethodInfo method) return method.GetRuntimeModule(); - else if (constructor != null) + else if (Member is RuntimeConstructorInfo constructor) return constructor.GetRuntimeModule(); - else if (property != null) + else if (Member is RuntimePropertyInfo property) return property.GetRuntimeModule(); else return null; } - public override int MetadataToken => m_tkParamDef; + public override int MetadataToken => _tkParamDef; public override Type[] GetRequiredCustomModifiers() { - return m_signature is null ? [] : m_signature.GetCustomModifiers(PositionImpl + 1, true); + return _signature is null ? [] : _signature.GetCustomModifiers(PositionImpl + 1, true); } public override Type[] GetOptionalCustomModifiers() { - return m_signature is null ? [] : m_signature.GetCustomModifiers(PositionImpl + 1, false); + return _signature is null ? [] : _signature.GetCustomModifiers(PositionImpl + 1, false); } public override Type GetModifiedParameterType() => - ModifiedType.Create(unmodifiedType: ParameterType, m_signature, parameterIndex: PositionImpl + 1); - #endregion + ModifiedType.Create(unmodifiedType: ParameterType, _signature, parameterIndex: PositionImpl + 1); } } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.NativeAot.cs index aae11ac53471aa..4c60238cab0e4c 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.NativeAot.cs @@ -12,10 +12,10 @@ namespace System.Reflection; internal sealed partial class RuntimeParameterInfo : ParameterInfo { - private readonly ParameterHandle m_tkParamDef; - private readonly MetadataReader? m_scope; - private readonly QSignatureTypeHandle m_signature; - private readonly TypeContext m_typeContext; + private readonly ParameterHandle _tkParamDef; + private readonly MetadataReader? _scope; + private readonly QSignatureTypeHandle _signature; + private readonly TypeContext _typeContext; internal static RuntimeParameterInfo[] GetParameters( ref TRuntimeMethodCommon method, MethodBase member, RuntimeTypeInfo[] methodTypeArguments) @@ -100,10 +100,10 @@ private RuntimeParameterInfo( PositionImpl = position; MemberImpl = member; - m_signature = signature; - m_typeContext = typeContext; - m_tkParamDef = parameterHandle; - m_scope = scope; + _signature = signature; + _typeContext = typeContext; + _tkParamDef = parameterHandle; + _scope = scope; AttrsImpl = attributes; } @@ -113,18 +113,18 @@ internal RuntimeParameterInfo(MemberInfo owner, Type parameterType, int position MemberImpl = owner; ClassImpl = parameterType; PositionImpl = position; - m_nameIsCached = true; + _nameIsCached = true; } private object? GetDefaultValueFromMetadata(bool raw) { - Debug.Assert(m_scope is not null && !m_tkParamDef.IsNil); + Debug.Assert(_scope is not null && !_tkParamDef.IsNil); Type parameterType = ParameterType; - Handle constantHandle = m_scope.GetParameter(m_tkParamDef).DefaultValue; + Handle constantHandle = _scope.GetParameter(_tkParamDef).DefaultValue; if (constantHandle.IsNil) return DBNull.Value; - object? value = constantHandle.ParseConstantValue(m_scope); + object? value = constantHandle.ParseConstantValue(_scope); if (parameterType.IsEnum && !raw) { if (value is null) @@ -150,27 +150,27 @@ internal RuntimeParameterInfo(MemberInfo owner, Type parameterType, int position return value; } - internal MetadataReader? GetMetadataReader() => m_scope; + internal MetadataReader? GetMetadataReader() => _scope; internal CustomAttributeHandleCollection GetCustomAttributeHandles() { - if (m_tkParamDef.IsNil) + if (_tkParamDef.IsNil) return default; - Debug.Assert(m_scope is not null); - return m_scope.GetParameter(m_tkParamDef).CustomAttributes; + Debug.Assert(_scope is not null); + return _scope.GetParameter(_tkParamDef).CustomAttributes; } - public override int MetadataToken => m_tkParamDef.IsNil + public override int MetadataToken => _tkParamDef.IsNil ? base.MetadataToken : throw new InvalidOperationException(SR.NoMetadataTokenAvailable); public override Type[] GetRequiredCustomModifiers() => - m_signature.Reader is null ? [] : m_signature.GetCustomModifiers(m_typeContext, optional: false); + _signature.Reader is null ? [] : _signature.GetCustomModifiers(_typeContext, optional: false); public override Type[] GetOptionalCustomModifiers() => - m_signature.Reader is null ? [] : m_signature.GetCustomModifiers(m_typeContext, optional: true); + _signature.Reader is null ? [] : _signature.GetCustomModifiers(_typeContext, optional: true); public override Type GetModifiedParameterType() => - m_signature.GetModifiedType(ParameterType); + _signature.GetModifiedType(ParameterType); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs index f65e1007c53d2d..8f293d5775c95b 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/RuntimeParameterInfo.cs @@ -16,26 +16,21 @@ namespace System.Reflection { internal sealed partial class RuntimeParameterInfo : ParameterInfo { - #region Private Data Members - private volatile bool m_nameIsCached; - private readonly bool m_noMetadata; - private bool m_noDefaultValue; - private readonly MethodBase? m_originalMember; - #endregion - - #region Internal Properties + private volatile bool _nameIsCached; + private readonly bool _noMetadata; + private bool _noDefaultValue; + private readonly MethodBase? _originalMember; + internal MethodBase DefiningMethod { get { - MethodBase? result = m_originalMember ?? MemberImpl as MethodBase; + MethodBase? result = _originalMember ?? MemberImpl as MethodBase; Debug.Assert(result != null); return result; } } - #endregion - #region Internal Methods internal void SetName(string? name) { NameImpl = name; @@ -45,17 +40,15 @@ internal void SetAttributes(ParameterAttributes attributes) { AttrsImpl = attributes; } - #endregion - #region Constructor // used by RuntimePropertyInfo internal RuntimeParameterInfo(RuntimeParameterInfo accessor, RuntimePropertyInfo property) : this(accessor, (MemberInfo)property) { #if NATIVEAOT - m_signature = property.GetParameterTypeHandle(PositionImpl); + _signature = property.GetParameterTypeHandle(PositionImpl); #else - m_signature = property.Signature; + _signature = property.Signature; #endif } @@ -66,12 +59,12 @@ private RuntimeParameterInfo(RuntimeParameterInfo accessor, MemberInfo member) // The original owner should always be a method, because this method is only used to // change the owner from a method to a property. - m_originalMember = accessor.MemberImpl as MethodBase; - Debug.Assert(m_originalMember != null); + _originalMember = accessor.MemberImpl as MethodBase; + Debug.Assert(_originalMember != null); // Populate all the caches -- we inherit this behavior from RTM NameImpl = accessor.Name; - m_nameIsCached = true; + _nameIsCached = true; ClassImpl = accessor.ParameterType; PositionImpl = accessor.Position; AttrsImpl = accessor.Attributes; @@ -79,17 +72,14 @@ private RuntimeParameterInfo(RuntimeParameterInfo accessor, MemberInfo member) // Strictly speaking, properties don't contain parameter tokens // However we need this to make ca's work... oh well... #if NATIVEAOT - m_tkParamDef = accessor.m_tkParamDef; - m_typeContext = accessor.m_typeContext; + _tkParamDef = accessor._tkParamDef; + _typeContext = accessor._typeContext; #else - m_tkParamDef = MdToken.IsNullToken(accessor.MetadataToken) ? (int)MetadataTokenType.ParamDef : accessor.MetadataToken; + _tkParamDef = MdToken.IsNullToken(accessor.MetadataToken) ? (int)MetadataTokenType.ParamDef : accessor.MetadataToken; #endif - m_scope = accessor.m_scope; + _scope = accessor._scope; } - #endregion - - #region Public Methods public override bool Equals(object? obj) => obj is RuntimeParameterInfo other && PositionImpl == other.PositionImpl && @@ -106,15 +96,15 @@ public override Type ParameterType if (ClassImpl == null) { #if NATIVEAOT - ClassImpl = m_signature.Resolve(m_typeContext).ToType(); + ClassImpl = _signature.Resolve(_typeContext).ToType(); #else - Debug.Assert(m_signature != null); + Debug.Assert(_signature != null); RuntimeType parameterType; if (PositionImpl == -1) - parameterType = m_signature.ReturnType; + parameterType = _signature.ReturnType; else - parameterType = m_signature.Arguments[PositionImpl]; + parameterType = _signature.Arguments[PositionImpl]; Debug.Assert(parameterType != null); // different thread could only write ClassImpl to the same value, so a race condition is not a problem here @@ -130,18 +120,18 @@ public override string? Name { get { - if (!m_nameIsCached) + if (!_nameIsCached) { - if (!MdToken.IsNullToken(m_tkParamDef)) + if (!MdToken.IsNullToken(_tkParamDef)) { - string name = m_scope.GetName(m_tkParamDef).ToString(); + string name = _scope.GetName(_tkParamDef).ToString(); GC.KeepAlive(this); NameImpl = name; } // other threads could only write it to true, so a race condition is OK // this field is volatile, so the write ordering is guaranteed - m_nameIsCached = true; + _nameIsCached = true; } // name may be null @@ -153,7 +143,7 @@ public override bool HasDefaultValue { get { - if (m_noMetadata || m_noDefaultValue) + if (_noMetadata || _noDefaultValue) return false; return TryGetDefaultValueInternal(false, out _); @@ -168,19 +158,17 @@ public override bool HasDefaultValue // OLD COMMENT (Is this even true?) // Cannot cache because default value could be non-agile user defined enumeration. // OLD COMMENT ends - if (m_noMetadata) + if (_noMetadata) return null; // for dynamic method we pretend to have cached the value so we do not go to metadata if (!TryGetDefaultValueInternal(raw, out object? defaultValue)) { - #region Handle case if no default value was found if (IsOptional) { // If the argument is marked as optional then the default value is Missing.Value. defaultValue = Missing.Value; } - #endregion } return defaultValue; @@ -223,22 +211,21 @@ public override bool HasDefaultValue // returns DBNull.Value if the parameter doesn't have a default value private bool TryGetDefaultValueInternal(bool raw, out object? defaultValue) { - Debug.Assert(!m_noMetadata); + Debug.Assert(!_noMetadata); - if (m_noDefaultValue || MdToken.IsNullToken(m_tkParamDef)) + if (_noDefaultValue || MdToken.IsNullToken(_tkParamDef)) { defaultValue = DBNull.Value; - m_noDefaultValue = true; + _noDefaultValue = true; return false; } // Prioritize metadata constant over custom attribute constant - #region Look for a default value in metadata - // This will return DBNull.Value if no constant value is defined on m_tkParamDef in the metadata. + // This will return DBNull.Value if no constant value is defined on _tkParamDef in the metadata. #if NATIVEAOT defaultValue = GetDefaultValueFromMetadata(raw); #else - defaultValue = MdConstant.GetValue(m_scope, m_tkParamDef, ParameterType.TypeHandle, raw); + defaultValue = MdConstant.GetValue(_scope, _tkParamDef, ParameterType.TypeHandle, raw); GC.KeepAlive(this); #endif @@ -256,14 +243,12 @@ private bool TryGetDefaultValueInternal(bool raw, out object? defaultValue) if (defaultValue == DBNull.Value) { - m_noDefaultValue = true; + _noDefaultValue = true; return false; } } return true; - - #endregion } private static decimal GetRawDecimalConstant(CustomAttributeData attr) @@ -309,12 +294,9 @@ private static DateTime GetRawDateTimeConstant(CustomAttributeData attr) return DBNull.Value; } - #endregion - - #region ICustomAttributeProvider public override object[] GetCustomAttributes(bool inherit) { - if (MdToken.IsNullToken(m_tkParamDef)) + if (MdToken.IsNullToken(_tkParamDef)) return []; return RuntimeCustomAttribute.GetCustomAttributes(this, (typeof(object) as RuntimeType)!); @@ -327,7 +309,7 @@ public override object[] GetCustomAttributes(Type attributeType, bool inherit) if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType) throw new ArgumentException(SR.Arg_MustBeType, nameof(attributeType)); - if (MdToken.IsNullToken(m_tkParamDef)) + if (MdToken.IsNullToken(_tkParamDef)) return RuntimeCustomAttribute.CreateAttributeArrayHelper(attributeRuntimeType, 0); return RuntimeCustomAttribute.GetCustomAttributes(this, attributeRuntimeType); @@ -337,7 +319,7 @@ public override bool IsDefined(Type attributeType, bool inherit) { ArgumentNullException.ThrowIfNull(attributeType); - if (MdToken.IsNullToken(m_tkParamDef)) + if (MdToken.IsNullToken(_tkParamDef)) return false; if (attributeType.UnderlyingSystemType is not RuntimeType attributeRuntimeType) @@ -348,15 +330,14 @@ public override bool IsDefined(Type attributeType, bool inherit) public override IList GetCustomAttributesData() { - if (MdToken.IsNullToken(m_tkParamDef)) + if (MdToken.IsNullToken(_tkParamDef)) return Array.Empty(); return RuntimeCustomAttributeData.GetCustomAttributesInternal(this); } - #endregion } -#if NATIVEAOT +#if NATIVEAOT file static class MdToken { public static bool IsNullToken(ParameterHandle token) => token.IsNil;