You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Some System.Reflection.Emit implementations of Type.IsByRefLike still throw NotSupportedException: "Derived classes must provide an implementation" #91532
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:
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:
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.
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.
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
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.
Activity
karakasa commentedon Sep 22, 2023
I went through the codebase and found 4 types not implementing
IsByRefLike. Should they all returnfalse?stakx commentedon Sep 22, 2023
I strongly suspect that the last one (
GenericTypeParameterBuilderImpl) at the very least should returnfalse, since by-ref-like types may not be used as generic type arguments... if that's what this class represents.MichalPetryka commentedon Sep 23, 2023
Worth noting that this might change, cc @jaredpar for progress on that
jaredpar commentedon Sep 25, 2023
In terms of updates on
ref structgeneric arguments ... for a long time I thought that effectively three features were tied together design wise:ref structgeneric arguments,ref structimplementing interfaces andref structasreffields.During the .NET 8 timeframe I was convinced that we can separate the design for
ref structasref 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.
buyaa-n commentedon Nov 1, 2023
Looks they should all return false, though I would leave
TypeBuilderImplandGenericTypeParameterBuilderImplout of this because they are new types that are not complete (many other unimplemented APIs not onlyIsByRefLike) and not exposed publicly yet`Type.IsByRefLike` is not always implemented
caraioniurie47 commentedon Sep 13, 2026
PR #133819 fixes this for all four types.
SymbolType,TypeBuilderImplandGenericTypeParameterBuilderImplreturnfalse.TypeBuilderInstantiationreturns its generic type definition's value, becausetypeof(Span<>).MakeGenericType(typeBuilder)also produces one and that type is by-ref-like.TypeBuilderImplandGenericTypeParameterBuilderImplare in a separate commit. They are theTypeBuilderandGenericTypeParameterBuilderimplementations used byPersistedAssemblyBuilder. They were suggested for exclusion here in 2023 as incomplete and not yet public;PersistedAssemblyBuilderhas 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
CustomReflectionContextin 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.
Implement `IsByRefLike` on the remaining Reflection.Emit `Type` subcl…