Repository navigation
Description
Background and motivation
Pointer.Box(void*, Type) stores a pointer with its type, but no public member returns that type; only the pointer comes back, through Pointer.Unbox. CoreLib reads the type internally: CoreCLR's MethodInfo.Invoke throws ArgumentException for a Pointer of long* passed to an int* parameter, and accepts any Pointer for a void* parameter.
Code outside CoreLib that turns a Pointer back into a pointer has no public API for that check. #136452 needs one: a Pointer that a DispatchProxy's Invoke hands back should be checked against the target type.
API Proposal
namespace System.Reflection;
public sealed partial class Pointer
{
// The pointer type of this instance, for example typeof(int*).
public Type PointerType { get; }
// The pointer stored in this instance.
public void* Value { get; }
}Value is there for completeness, as jkotas suggested.
API Usage
Unboxing a Pointer for a parameter of type targetType with the check CoreCLR's MethodInfo.Invoke makes:
static unsafe void* UnboxPointer(object value, Type targetType)
{
Pointer pointer = (Pointer)value;
Type pointerType = pointer.PointerType;
if (targetType != typeof(void*) && !pointerType.IsAssignableTo(targetType))
{
throw new ArgumentException($"A Pointer of type '{pointerType}' can't be converted to '{targetType}'.");
}
return pointer.Value;
}Alternative Designs
- An instance method,
public Type GetPointerType(), as first proposed here. A property matchesFieldInfo.FieldTypeandParameterInfo.ParameterType. public static Type GetPointerType(object ptr), static likeUnbox(object ptr)andTypedReference.GetTargetType, throwingArgumentExceptionwhenptris not aPointer.- A checked overload,
public static void* Unbox(object ptr, Type type), that throws when the stored type doesn't fittype. It would make the conversion rule public API: whethervoid*takes any pointer, whether an assignable type is enough. The properties only expose whatPointeralready stores, and such an overload could be added on top of them later. - Private reflection or
UnsafeAccessoron the existing internalGetPointerType(): both depend on a non-public member.
Risks
None known: Pointer is sealed.
Candidate: DispatchProxy, in #136452. Outside Pointer.cs, no code in src/libraries reads a Pointer's type on main (a5ea4c6).
I can turn the prototype into a PR once this is approved, then add the check to #136452. Could this be assigned to me?
Note
AI-generated, written at my direction and reviewed by me before posting. The prototype's System.Runtime and System.Reflection tests and the usage example ran on its CoreCLR build, Windows x64.
Activity
AaronRobinsonMSFT commentedon Nov 13, 2026
If serialization is the only argument here, I think I'd prefer the property approach. @jkotas ?
caraioniurie47 commentedon Nov 13, 2026
Yes, serialization is the only argument for the method. A property has two more things going for it: the internal
GetPointerType()can stay, so the private reflection andUnsafeAccessorbreaks under Risks go away, and it matchesFieldInfo.FieldTypeandParameterInfo.ParameterType.AaronRobinsonMSFT commentedon Nov 13, 2026
These should never be considered "risks" for this sort of thing. Appreciate the diligence thinking through cases.
jkotas commentedon Nov 14, 2026
I agree that this should be a property. None of concerns raised are valid. Also, I think we may want to add
public void* Value { get; }property as well for completeness. (I understand you extract the value usingUnboxtoday.)[-][API Proposal]: Expose the type stored in a System.Reflection.Pointer[/-][+][API Proposal]: Expose the type and value stored in a System.Reflection.Pointer[/+]caraioniurie47 commentedon Nov 14, 2026
Updated the proposal to the two properties,
PointerTypeandValue, and the prototype with them.dotnet-policy-service commentedon Nov 14, 2026
Tagging subscribers to this area: @steveisok, @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.
bartonjs commentedon Nov 17, 2026
Video
Looks good as proposed.
Add Pointer.PointerType and Pointer.Value (#137769)