Skip to content

[API Proposal]: Add managed API surface area for ByRefLike Generic constraints #68002

@AaronRobinsonMSFT

Description

@AaronRobinsonMSFT

Background and motivation

The design for ByRefLike Generic constraints requires some new API surface area. Specifically the need to represent the new metadata values and a new feature flag to indicate support for the compiler.

API Proposal

Metadata enum values:

namespace System.Reflection
{
    [Flags]
    public enum GenericParameterAttributes
    {
+        [Obsolete("No longer represents all special constraints")]
         SpecialConstraintMask = 0x001C,
+        AcceptByRefLike = 0x0020,
    }
}

Feature flag.

namespace System.Runtime.CompilerServices
{
    public static partial class RuntimeFeature
    {
+        /// <summary>
+        /// Represents a runtime feature where byref-like types can be used in Generic parameters.
+        /// </summary>
+        public const string GenericsAcceptByRefLike = nameof(GenericsAcceptByRefLike);
    }
}

API Usage

Usage is similar to the official documentation for any of the existing GenericParameterAttributes enumeration values.

Alternative Designs

The following would represent a binary breaking change.

namespace System.Reflection
{
    [Flags]
    public enum GenericParameterAttributes
    {
-        SpecialConstraintMask = 0x001C,
+        SpecialConstraintMask = 0x003C,
+        AcceptByRefLike = 0x0020,
    }
}

Risks

Updating the enumeration value is troublesome since it contains a value that is a mask—an established anti-pattern. The "safest" way to handle this is the initial proposal but that does introduce confusion about new scenarios. It might be prudent to create a new API for accessing the specific constraints from the variance values.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions