Skip to content

GenAPI needs to account for ref fields  #63751

@jaredpar

Description

@jaredpar

Support for ref fields is being added in .NET 7 / C# 11.0. This will impact reference assembly generation as ref fields impact how C# consumes types that contain them.

Ideally a ref field in a reference assembly would just appear as a normal field of the same type would appear but with the ref modifier. However a ref field represents a change to the metadata format and that can cause issues with tool chains that are not updated to understand this metadata change. A concrete example is C++/CLI which will likely error if it consumes a ref field.

This means it's advantageous if ref fields can be omitted from reference assemblies in our core libraries. That removes an ask on the C++/CLI team in the short term and possibly avoids issues in other toolsets.

The semantics of how we need to describe a ref field in a reference assembly are described in the ref field proposal. In short though when omitting ref we need to be careful to maintain the other properties it conveys:

  • The containing type can never be considered unmanaged
  • The type of the field, sans ref, is still important for generic expansion calculations

A method for achieving that is as follows:

// Impl assembly 
ref struct S<T> {
    ref T _field;
}

// Ref assembly 
ref struct S<T> {
    object _o; // force managed 
    T _f; // maintain generic expansion protections
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions