Repository navigation
Description
Description
Types mapped by a CustomReflectionContext, and the members they return, throw NotImplementedException from HasSameMetadataDefinitionAs. No class in System.Reflection.Context overrides it, so the MemberInfo base runs. Type.GetMemberWithSameMetadataDefinitionAs calls it on each member of the type, so it throws too, including for its main use: finding a member of a constructed generic type from the member of its definition.
In main at be8eaf9, MemberInfo.cs:32:
public virtual bool HasSameMetadataDefinitionAs(MemberInfo other) { throw NotImplemented.ByDesign; }public virtual MemberInfo GetMemberWithSameMetadataDefinitionAs(MemberInfo member)
{
ArgumentNullException.ThrowIfNull(member);
const BindingFlags all = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance;
foreach (MemberInfo myMemberInfo in GetMembers(all))
{
if (myMemberInfo.HasSameMetadataDefinitionAs(member))
{
return myMemberInfo;
}
}
throw CreateGetMemberWithSameMetadataDefinitionAsNotFoundException(member);
}Reproduction Steps
net10.0 console app referencing the System.Reflection.Context 10.0.12 package:
using System;
using System.Reflection;
using System.Reflection.Context;
var context = new MyContext();
Type definition = context.MapType(typeof(C<>).GetTypeInfo());
Type constructed = context.MapType(typeof(C<int>).GetTypeInfo());
FieldInfo field = definition.GetField("F")!;
Run(() => definition.HasSameMetadataDefinitionAs(constructed));
Run(() => field.HasSameMetadataDefinitionAs(field));
Run(() => constructed.GetMemberWithSameMetadataDefinitionAs(field));
static void Run(Func<object> f)
{
try { Console.WriteLine(f()); }
catch (Exception e) { Console.WriteLine(e.GetType()); }
}
class C<T>
{
public T F = default!;
}
class MyContext : CustomReflectionContext { }Expected behavior
The same answers as for the runtime types and members: True, True, Int32 F.
Actual behavior
System.NotImplementedException three times. It also throws on mapped methods, constructors, properties, events and nested types, on properties the context adds and their accessors, and on a mapped type given the runtime type as the argument.
Regression?
No. git log -S HasSameMetadataDefinitionAs over src/libraries/System.Reflection.Context finds no commit: the library has not overridden it since it was ported in 2018.
Known Workarounds
For types, compare UnderlyingSystemType, which is the runtime type. For mapped members, compare MetadataToken and Module, which is what the runtime's own implementation does. That doesn't work for properties the context adds, whose MetadataToken throws InvalidOperationException.
Configuration
.NET 10.0.12, System.Reflection.Context 10.0.12, Windows 11 x64.
Other information
Proposal: override HasSameMetadataDefinitionAs in the projecting type and member classes, comparing the members they wrap and matching only members mapped by the same context. That is what their Equals does, and what the runtime's members do: they match only members of their own classes, so runtimeType.HasSameMetadataDefinitionAs(mappedType) is already False. MemberInfo.Internal.cs:14-16 (CoreCLR):
// Ensure that "other" is a runtime-implemented MemberInfo. Do this check before calling any methods on it!
if (other is not TOther)
return false;A property the context adds has no metadata definition. The proposal is that it, and each of its accessors, matches only itself, including the copy a derived mapped type reports with a different ReflectedType. GetMemberWithSameMetadataDefinitionAs then needs no override of its own.
PR #134371, which forwarded other Type members in this library, listed this as not included. Searching issues and PRs for HasSameMetadataDefinitionAs and GetMemberWithSameMetadataDefinitionAs found no report of it.
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) with the 10.0.12 package; a scratch app on the same runtime and package gave the other member kinds, the runtime-type result, and the workarounds. Code excerpts are from main at be8eaf9. The regression answer comes from git log -S over the library's directory, not from running older releases.
Activity
dotnet-policy-service commentedon Oct 5, 2026
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.
Implement `HasSameMetadataDefinitionAs` in System.Reflection.Context (#…