Skip to content

[API Proposal]: Expose the type and value stored in a System.Reflection.Pointer #137512

Closed

Description

@caraioniurie47

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 matches FieldInfo.FieldType and ParameterInfo.ParameterType.
  • public static Type GetPointerType(object ptr), static like Unbox(object ptr) and TypedReference.GetTargetType, throwing ArgumentException when ptr is not a Pointer.
  • A checked overload, public static void* Unbox(object ptr, Type type), that throws when the stored type doesn't fit type. It would make the conversion rule public API: whether void* takes any pointer, whether an assignable type is enough. The properties only expose what Pointer already stores, and such an overload could be added on top of them later.
  • Private reflection or UnsafeAccessor on the existing internal GetPointerType(): 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

  1. added
    untriaged
    on Nov 13, 2026
  2. AaronRobinsonMSFT commented on Nov 13, 2026

    @AaronRobinsonMSFT
    Member

    A property, public Type PointerType { get; }. System.Text.Json would then serialize it: serializing a Pointer writes {} today and would throw NotSupportedException, as it does for any System.Type value.

    If serialization is the only argument here, I think I'd prefer the property approach. @jkotas ?

  3. caraioniurie47 commented on Nov 13, 2026

    @caraioniurie47
    ContributorAuthor

    A property, public Type PointerType { get; }. System.Text.Json would then serialize it: serializing a Pointer writes {} today and would throw NotSupportedException, as it does for any System.Type value.

    If serialization is the only argument here, I think I'd prefer the property approach. @jkotas ?

    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 and UnsafeAccessor breaks under Risks go away, and it matches FieldInfo.FieldType and ParameterInfo.ParameterType.

  4. AaronRobinsonMSFT commented on Nov 13, 2026

    @AaronRobinsonMSFT
    Member

    so the private reflection and UnsafeAccessor breaks under Risks go away

    These should never be considered "risks" for this sort of thing. Appreciate the diligence thinking through cases.

  5. added
    api-suggestion
    on Nov 13, 2026
  6. jkotas commented on Nov 14, 2026

    @jkotas
    Member

    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 using Unbox today.)

  7. changed the title [-][API Proposal]: Expose the type stored in a System.Reflection.Pointer[/-] [+][API Proposal]: Expose the type and value stored in a System.Reflection.Pointer[/+] on Nov 14, 2026
  8. caraioniurie47 commented on Nov 14, 2026

    @caraioniurie47
    ContributorAuthor

    Updated the proposal to the two properties, PointerType and Value, and the prototype with them.

  9. added
    api-ready-for-review
    and removed
    api-suggestion
    on Nov 14, 2026
  10. dotnet-policy-service commented on Nov 14, 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.

  11. removed
    untriaged
    on Nov 16, 2026
  12. added this to the 12.0.0 milestone on Nov 16, 2026
  13. bartonjs commented on Nov 17, 2026

    @bartonjs
    Member

    Video

    Looks good as proposed.

    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; }
    }
  14. added
    api-approved
    and removed
    api-ready-for-review
    on Nov 17, 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

    api-approvedarea-System.Reflection

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions