Skip to content

TypeBuilder (AssemblyBuilderAccess.Run) throws NotSupportedException for signatures that contain modified types #136449

Description

@caraioniurie47

Description

With the runtime AssemblyBuilder (AssemblyBuilderAccess.Run), TypeBuilder.CreateType throws NotSupportedException when a method signature contains a modified type: a type returned by ParameterInfo.GetModifiedParameterType(), FieldInfo.GetModifiedFieldType() or Type.MakeModifiedSignatureType. DefineMethod itself returns; the exception comes when the signature is encoded. Constructor, field and property signatures fail the same way, but DefineConstructor, DefineField and DefineProperty throw it themselves, since they encode the signature at once.

PersistedAssemblyBuilder accepts the same calls, so the two emitters disagree. DispatchProxy builds its proxy types with the runtime TypeBuilder, so it can't copy a modifier nested inside a parameter type, such as int32 modopt(IsConst)*, until TypeBuilder accepts modified types (#100188, #135567).

The runtime emitter's SignatureHelper has no case for a modified type. It is not a RuntimeType, so it reaches the module's type-token lookup, which asks it for members a modified type does not support (SignatureType.Module, ModifiedType.DeclaringType):

SignatureHelper.cs:513-528

                if (IsSimpleType(type))
                {
                    AddElementType(type);
                }
                else if (m_module == null)
                {
                    InternalAddRuntimeType(clsArgument);
                }
                else if (clsArgument.IsValueType)
                {
                    InternalAddTypeToken(m_module.GetTypeMetadataToken(clsArgument), CorElementType.ELEMENT_TYPE_VALUETYPE);
                }
                else
                {
                    InternalAddTypeToken(m_module.GetTypeMetadataToken(clsArgument), CorElementType.ELEMENT_TYPE_CLASS);
                }

Reproduction Steps

With each emitter, the program defines a method static void M whose parameter has a type from Type.MakeModifiedSignatureType (two modreqs), then again with the type of a volatile field from GetModifiedFieldType(), and prints the modreqs reflection reads back from the created method.

repro.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net11.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
</Project>

Program.cs:

using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;

Type madeType = Type.MakeModifiedSignatureType(typeof(int), [typeof(IsConst), typeof(IsVolatile)], null);
Type reflectedType = typeof(Holder).GetField(nameof(Holder.Value))!.GetModifiedFieldType();

foreach ((string name, Type parameterType) in new[] { ("MakeModifiedSignatureType", madeType), ("GetModifiedFieldType", reflectedType) })
{
    Console.WriteLine($"{name}, AssemblyBuilder: {DefineWithAssemblyBuilder(parameterType)}");
    Console.WriteLine($"{name}, PersistedAssemblyBuilder: {DefineWithPersistedAssemblyBuilder(parameterType)}");
}

// Defines static void M(parameterType) and returns the modreqs reflection reads back from the created method.
static string DefineWithAssemblyBuilder(Type parameterType)
{
    ModuleBuilder module = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Run"), AssemblyBuilderAccess.Run).DefineDynamicModule("Run");
    TypeBuilder type = DefineType(module, parameterType);
    try
    {
        return Modreqs(type.CreateType());
    }
    catch (Exception e)
    {
        return $"{e.GetType()}: {e.Message}";
    }
}

static string DefineWithPersistedAssemblyBuilder(Type parameterType)
{
    PersistedAssemblyBuilder assembly = new(new AssemblyName("Saved"), typeof(object).Assembly);
    DefineType(assembly.DefineDynamicModule("Saved"), parameterType).CreateType();
    using MemoryStream stream = new();
    assembly.Save(stream);
    stream.Position = 0;
    return Modreqs(new AssemblyLoadContext("Saved").LoadFromStream(stream).GetType("T")!);
}

static TypeBuilder DefineType(ModuleBuilder module, Type parameterType)
{
    TypeBuilder type = module.DefineType("T", TypeAttributes.Public);
    MethodBuilder method = type.DefineMethod("M", MethodAttributes.Public | MethodAttributes.Static, typeof(void), [parameterType]);
    method.GetILGenerator().Emit(OpCodes.Ret);
    return type;
}

static string Modreqs(Type created) =>
    "modreqs [" + string.Join(", ", created.GetMethod("M")!.GetParameters()[0].GetModifiedParameterType().GetRequiredCustomModifiers().Select(t => t.Name)) + "]";

class Holder
{
    public static volatile int Value = 1;
}

Expected behavior

Both emitters accept the modified types, and the created method's parameter reads back with the modifiers it was defined with, as with PersistedAssemblyBuilder:

MakeModifiedSignatureType, AssemblyBuilder: modreqs [IsConst, IsVolatile]
MakeModifiedSignatureType, PersistedAssemblyBuilder: modreqs [IsConst, IsVolatile]
GetModifiedFieldType, AssemblyBuilder: modreqs [IsVolatile]
GetModifiedFieldType, PersistedAssemblyBuilder: modreqs [IsVolatile]

Actual behavior

MakeModifiedSignatureType, AssemblyBuilder: System.NotSupportedException: This method is not supported on signature types.
MakeModifiedSignatureType, PersistedAssemblyBuilder: modreqs [IsConst, IsVolatile]
GetModifiedFieldType, AssemblyBuilder: System.NotSupportedException: Modified types do not support this member. Use the UnderlyingSystemType property to call this member.
GetModifiedFieldType, PersistedAssemblyBuilder: modreqs [IsVolatile]

Regression?

Not from .NET 10: the GetModifiedFieldType case fails the same way on .NET 10.0.12. Earlier releases not checked. Type.MakeModifiedSignatureType was added in #123819.

Known Workarounds

Pass UnderlyingSystemType and the modifiers through the DefineMethod overload that takes modifier arrays. That overload writes each array in array order, while reflection returns the modifiers of each kind in the reverse of their signature order, so reflected arrays have to be reversed first. It also writes all optional modifiers before all required ones, so a signature with a required modifier before an optional one can't be reproduced this way.

Configuration

Reproduced on Ubuntu 24.04.5 LTS x64 and Windows 11 x64, with the .NET 11.0.0-rc.1.26420.103 runtime and with a CoreCLR build of main. Not checked on other platforms. #135539 reports that on Mono, CreateType ends the process for a method with a ModifiedType parameter.

Other information

Proposed direction: in a TypeBuilder's member, local and calli signatures, CoreCLR's SignatureHelper writes a modified type's custom modifiers and then the unmodified type, also for modified element and generic argument types. Required modifiers come before optional ones; each kind in signature order (the reverse of the order reflection returns them), and a modifier array passed together with a modified type replaces the type's own modifiers of that kind, its elements given in the order reflection returns. A type token (such as an ldtoken or unbox.any operand) takes a modified type's unmodified type; a modified type used to construct another type keeps its modifiers in that type's token, as in a member signature. Signatures without modified types and signatures built without a module, such as a DynamicMethod's EmitCalli signature, are encoded as before.

Reflection returns the required and the optional modifiers as two separate arrays, so a signature with an optional modifier before a required one at the same position comes out reordered.

Options considered and dropped:

  • Read the order of the two kinds from the signature a modified type was read from: it would reach into ModifiedType internals; keeping that order would need new System.Type API.
  • Replace a modified type in a member signature by its UnderlyingSystemType and drop its modifiers (what DynamicMethod does with its parameter types): the defined signature would silently differ from the one it was copied from.
  • Write the modifiers in the order reflection returns them: two modifiers of one kind would come out reversed compared with the source signature and with PersistedAssemblyBuilder.
  • Write both a modifier array and the type's own modifiers of that kind: the same call would produce a different signature than PersistedAssemblyBuilder.
  • Throw a clearer exception instead: the two emitters would still disagree.
  • Also accept modified types in signatures without a module, such as a DynamicMethod's EmitCalli: a separate behaviour change, for a different emitter.

I have a fix with a test ready and will open a PR for it shortly. Could this be assigned to me?

Note

AI-generated, written at my direction and reviewed by me before posting.
Source read at dotnet/runtime 716a871. The repro was built with dotnet build -c Release and run with dotnet bin/Release/net11.0/repro.dll on Ubuntu 24.04.5 LTS x64, on the .NET 11.0.0-rc.1.26420.103 runtime and on a ./build.sh clr+libs -rc Release build of that commit (testhost dotnet, DOTNET_ROLL_FORWARD=LatestMajor). On Windows 11 x64 it was built the same way and run on .NET 11.0.0-rc.1.26420.103 and on the Release testhost of an earlier local build of main (DOTNET_ROLL_FORWARD=Major, DOTNET_ROLL_FORWARD_TO_PRERELEASE=1), and its GetModifiedFieldType half on .NET 10.0.12.

Activity

  1. added
    untriaged
    on Oct 25, 2026
  2. dotnet-policy-service commented on Oct 25, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
    See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Reflectionuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions