Skip to content

New Type System.Runtime.CompilerServices.DependentHandle<TPrimary, TSecondary> #19459

Description

@shmuelie

Background and Motivation

There is an internal struct System.Runtime.CompilerServices.DependentHandle that is currently only used by System.Runtime.CompilerServices.ConditionalWeakTable<TKey, TValue>. The struct allows for a dependency between two objects to be created by a third type without modifying the first two classes, and only using up a pointer's worth of memory in the third type.

I have personally find use for such a class, in my Weak Delegates system for example. While in there I had to use slightly convoluted tricks to make it work I believe a more proper implementation included in the BCL would be useful.

Proposed API

namespace System.Runtime.CompilerServices
{
    public struct DependentHandle
    {
        public DependentHandle(object? target, object? dependent);

        public bool IsAllocated { get; }

        public object? Target { get; set; }
        public object? Dependent { get; set; }

        (object? Target, object? Dependent) TargetAndDependent { get; }

        public void Free();
    }
}

Open Questions

Should DependentHandle implement IDisposable? If so, we should expose the same on GCHandle

Should the constructor be private and have a static Alloc method to match GCHandle?

Should TargetAndDependent be a property or a method?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions