Skip to content

Some System.Reflection.Emit implementations of Type.IsByRefLike still throw NotSupportedException: "Derived classes must provide an implementation" #91532

Closed

Description

@stakx

Description

Certain System.Reflection.Emit (SRE) subclasses of Type appear to be missing an implementation for the Type.IsByRefLike property. When queried, a NotSupportedException ("Derived classes must provide an implementation") gets thrown when they should probably just return false.

I've so far found two such types that both seem to involve generics: System.Reflection.Emit.TypeBuilderInstantiation and System.Reflection.Emit.SymbolType.

Could it be that these two types were overlooked back in #34846?

Reproduction Steps

Here is one code example that reproduces the problem for System.Reflection.Emit.TypeBuilderInstantiation:

var assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("A"), AssemblyBuilderAccess.Run);
var moduleBuilder = assemblyBuilder.DefineDynamicModule("A");
var genericTypeBuilder = moduleBuilder.DefineType("C", TypeAttributes.Class);
_ = genericTypeBuilder.DefineGenericParameters("T");
var closedGenericTypeBuilder = genericTypeBuilder.MakeGenericType(typeof(object));
Console.WriteLine(closedGenericTypeBuilder.GetType().FullName);
Console.WriteLine(closedGenericTypeBuilder.IsByRefLike);

(I haven't yet been able to create a short code repro for System.Reflection.Emit.SymbolType, but will add one if I can figure it out.)

Expected behavior

Prints the following to the console:

System.Reflection.Emit.TypeBuilderInstantiation
False

Actual behavior

Prints the following to the console:

System.Reflection.Emit.TypeBuilderInstantiation
Unhandled exception. System.NotSupportedException: Derived classes must provide an implementation.
   at System.Type.get_IsByRefLike()
   at Program.Main ...

The exception is caused by the IsByRefLike query in the last line of code.

Regression?

I don't think this is a regression, since the same error also occurs on older .NET runtimes (e. g. .NET Core 3.1). It appears like this simply hasn't been implemented yet at all.

Known Workarounds

I don't know of any workaround short of wrapping IsByRefLike queries in an extension method that catches the exception and converts it to false, then calling that extension method instead of using IsByRefLike directly:

static bool IsByRefLikeSafe(this Type type)
{
    try
    {
        return type.IsByRefLike;
    }
    catch (NotSupportedException)
    {
        return false;
    }
}

Configuration

  • I've run the code repro on the following runtimes:
    • .NET Core 3.1
    • .NET 6
    • .NET 7
  • OS: Windows 11 Pro (22H2), build 22621.2134
  • Architecture: x64

Other information

No response

Activity

  1. ghost added
    untriaged
    on Sep 4, 2023
  2. added and removed
    untriaged
    on Sep 5, 2023
  3. added this to the Future milestone on Sep 5, 2023
  4. added
    help wanted
    on Sep 5, 2023
  5. karakasa commented on Sep 22, 2023

    @karakasa
    Contributor

    I went through the codebase and found 4 types not implementing IsByRefLike. Should they all return false?

    System.Reflection.Emit.TypeBuilderInstantiation
    System.Reflection.Emit.SymbolType
    System.Reflection.Emit.TypeBuilderImpl
    System.Reflection.Emit.GenericTypeParameterBuilderImpl
    
  6. stakx commented on Sep 22, 2023

    @stakx
    ContributorAuthor

    I strongly suspect that the last one (GenericTypeParameterBuilderImpl) at the very least should return false, since by-ref-like types may not be used as generic type arguments... if that's what this class represents.

  7. MichalPetryka commented on Sep 23, 2023

    @MichalPetryka
    Contributor

    since by-ref-like types may not be used as generic type arguments

    Worth noting that this might change, cc @jaredpar for progress on that

  8. jaredpar commented on Sep 25, 2023

    @jaredpar
    Member

    In terms of updates on ref struct generic arguments ... for a long time I thought that effectively three features were tied together design wise: ref struct generic arguments, ref struct implementing interfaces and ref struct as ref fields.

    During the .NET 8 timeframe I was convinced that we can separate the design for ref struct as ref fields. That's fortunate because it's probably the one with the most open questions. Basically design issues where there are presently no good answers. The other two features have some challenges but they're more in realm of deciding which design is best. If we committed to the feature I'm pretty confident we could ship it in a .NET release.

    So at this point it's more a question of where it fits in the priority ranking.

  9. buyaa-n commented on Nov 1, 2023

    @buyaa-n
    Contributor

    I went through the codebase and found 4 types not implementing IsByRefLike. Should they all return false?

    System.Reflection.Emit.TypeBuilderInstantiation
    System.Reflection.Emit.SymbolType
    System.Reflection.Emit.TypeBuilderImpl
    System.Reflection.Emit.GenericTypeParameterBuilderImpl
    

    Looks they should all return false, though I would leave TypeBuilderImpl and GenericTypeParameterBuilderImpl out of this because they are new types that are not complete (many other unimplemented APIs not only IsByRefLike) and not exposed publicly yet

  10. added
    in-pr
    on Sep 13, 2026
  11. caraioniurie47 commented on Sep 13, 2026

    @caraioniurie47
    Contributor

    PR #133819 fixes this for all four types. SymbolType, TypeBuilderImpl and GenericTypeParameterBuilderImpl return false. TypeBuilderInstantiation returns its generic type definition's value, because typeof(Span<>).MakeGenericType(typeBuilder) also produces one and that type is by-ref-like.

    TypeBuilderImpl and GenericTypeParameterBuilderImpl are in a separate commit. They are the TypeBuilder and GenericTypeParameterBuilder implementations used by PersistedAssemblyBuilder. They were suggested for exclusion here in 2023 as incomplete and not yet public; PersistedAssemblyBuilder has been public since #99660, and that commit can be dropped if they should still stay out.

    The same exception comes from types mapped by a CustomReflectionContext in System.Reflection.Context; that is #133816.

    Note

    AI-generated, written at my direction and reviewed by me before posting. The behaviour described is covered by the tests in the PR, run on local checked and release builds of that branch on Windows x64.

  12. added a commit that references this issue on Oct 6, 2026
    4a52eee
  13. locked and limited conversation to collaborators on Nov 8, 2026
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.Reflection.Emitbughelp wantedin-pr

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions