Skip to content

[API Proposal]: Simplify lookup of parsed TypeName in metadata #101774

Closed
@jkotas

Description

@jkotas

Background and motivation

The recently introduced System.Reflection.Metadata.TypeName is likely to be frequently combined with lookup of the type in metadata. The information required to lookup a type in ECMA335 metadata tables is unescaped (namespace, name) tuple. This information is not provided by the TypeName type today. In order to lookup the parser type name in metadata, the consumers have to implement unescaping and full type name splitting helper methods to convert the information provided TypeName this tuple.

API Proposal

  namespace System.Reflection.Metadata;

  public class TypeName
  {
      // Existing APIs. Return escaped names (to match reflection), no API to get Namespace only.
      public string Name { get; }
      public string FullName { get; }
      
      // Returns escaped namespace. (Same API exists on System.Type today.)
+    public string Namespace { get; }

      // Unescapes 
+    public static string Unescape(string name);
  }

API Usage

CustomType ResolveType(TypeName typeName)
{
    if (typeName.IsSimple)
    {
        if (typeName.IsNested)
        {
            string nestedName = TypeName.Unescape(typeName.Name);
            ... use nestedName to lookup the type in metadata ...
        }
        else
        {
            string typeNamespace = TypeName.Unescape(typeName.Namespace);
            string typeName = TypeName.Unescape(typeName.Name);
            ... use typeNamespace and typeName to lookup the type in metadata ...
        }
    }

    ... handle other cases ...
}

Alternative Designs

Alt option 1: Introduce a method that returns an unescaped (namespace, name) tuple.

Alt option 2: Change the Namespace and Name methods to return unescaped names (different from reflection). It would avoid need to introduce Unescape method.

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedAPI was approved in API review, it can be implementedarea-System.Reflection.Metadatain-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions