Repository navigation
Description
Description
A TypeDelegator wrapping a modified type (from GetModifiedFieldType(), GetModifiedParameterType() and similar) reports no custom modifiers: GetRequiredCustomModifiers() and GetOptionalCustomModifiers() fall through to Type's []. GetTypeInfo() on a modified type returns such a TypeDelegator, so modifiedType.GetTypeInfo() loses the modifiers too.
#81006 added both methods to Type and, in the same commit, the function-pointer forwards to TypeDelegator, but not these two. In System.Private.CoreLib at d4b2951:
Type.cs:82-83 returns [] from both:
public virtual Type[] GetOptionalCustomModifiers() => [];
public virtual Type[] GetRequiredCustomModifiers() => [];ModifiedType.cs:56-66 overrides both:
public override Type[] GetRequiredCustomModifiers()
{
// No caching is performed; as is the case with FieldInfo.GetCustomModifiers and friends.
return GetCustomModifiers(required: true);
}
public override Type[] GetOptionalCustomModifiers()
{
// No caching is performed; as is the case with FieldInfo.GetCustomModifiers and friends.
return GetCustomModifiers(required: false);
}TypeDelegator.cs forwards the function-pointer members (:93-95, :160-161) but neither custom-modifier method:
public override Type[] GetFunctionPointerCallingConventions() => typeImpl.GetFunctionPointerCallingConventions();
public override Type[] GetFunctionPointerParameterTypes() => typeImpl.GetFunctionPointerParameterTypes();
public override Type GetFunctionPointerReturnType() => typeImpl.GetFunctionPointerReturnType();
public override bool IsFunctionPointer => typeImpl.IsFunctionPointer;
public override bool IsUnmanagedFunctionPointer => typeImpl.IsUnmanagedFunctionPointer;IntrospectionExtensions.cs:12-20 wraps any type that isn't an IReflectableType, which ModifiedType isn't:
public static TypeInfo GetTypeInfo(this Type type)
{
ArgumentNullException.ThrowIfNull(type);
if (type is IReflectableType reflectableType)
return reflectableType.GetTypeInfo();
return new TypeDelegator(type);
}Reproduction Steps
net10.0 console app:
using System;
using System.Reflection;
Type modified = typeof(C).GetField(nameof(C.F))!.GetModifiedFieldType();
Type delegator = new TypeDelegator(modified);
Console.WriteLine(modified.GetRequiredCustomModifiers().Length); // 1 (IsVolatile)
Console.WriteLine(delegator.GetRequiredCustomModifiers().Length); // 0
class C
{
public volatile int F;
}Expected behavior
The delegator reports the modifiers of the type it wraps: 1, then 1.
Actual behavior
1, then 0. typeof(C).GetField("F")!.GetModifiedFieldType().GetTypeInfo() is a TypeDelegator, and its GetRequiredCustomModifiers() also returns [].
Regression?
No. Type gained both methods in #81006, which is in .NET 8, and TypeDelegator has never overridden them.
Known Workarounds
Call the methods on the modified type itself, not on GetTypeInfo()'s result or a TypeDelegator over it. Code that creates its own delegators can subclass TypeDelegator and forward both methods to the protected typeImpl. UnderlyingSystemType doesn't help: on the delegator it returns the unmodified runtime type (System.Int32 in the repro), which has no modifiers.
Configuration
.NET 10.0.12, Windows 11 x64.
Other information
Proposal: add the two forwards to TypeDelegator, as it does for the function-pointer members, with the matching System.Runtime ref entries and a test in TypeDelegatorTests. For a TypeDelegator over a RuntimeType nothing changes, since RuntimeType returns [] for both.
Searching issues and PRs for TypeDelegator with CustomModifiers or modifiers found no existing report.
I'll send a PR with this change. Could this be assigned to me?
Note
AI-generated, written at my direction and reviewed by me before posting. The repro was compiled and run as written on .NET 10.0.12 (Windows x64). Scratch apps on the same runtime gave the GetTypeInfo() result, the workarounds, and the same loss for parameter and property modified types and for optional modifiers. Code excerpts are from main at d4b2951. The regression answer comes from git log -S over Type.cs and TypeDelegator.cs, plus git merge-base against release/8.0, not from running older releases.
Activity
dotnet-policy-service commentedon Sep 23, 2026
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.
Forward custom modifiers in `TypeDelegator` (#134553)