Skip to content

Make MetadataDisplayFormat public #72844

Open

Description

@tmat

Background and Motivation

After a compilation is emitted and the resulting image is loaded, we need to find a type in the loaded Assembly that corresponds to a source INamedTypeSymbol defined in the compilation.
 

Proposed API

namespace Microsoft.CodeAnalysis;

public class SymbolDisplayFormat
{
+   SymbolDisplayFormat MetadataDisplayFormat { get; }
}

Implemented like so:

private static readonly SymbolDisplayFormat s_metadataDisplayFormat =
SymbolDisplayFormat.QualifiedNameArityFormat.AddCompilerInternalOptions(SymbolDisplayCompilerInternalOptions.UsePlusForNestedTypes);

Usage Examples

var qualifiedTypeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.MetadataDisplayFormat);
var type = Assembly.GetType(qualifiedTypeName);

Alternative Designs

Risks

Activity

  1. added
    Concept-API
    on Apr 2, 2024
  2. ghost added
    untriaged
    on Apr 2, 2024
  3. added
    api-ready-for-review
    on Apr 2, 2024
  4. added this to the 17.11 milestone on Apr 5, 2024
  5. 333fred commented on Apr 11, 2024

    @333fred
    Member

    API Review

    • Rather than exposing a new MetadataDisplayFormat, can we just expose UsePlusForNestedTypes option?
    • Let users then create the format they need
    • What part of metadata format does this cover? Is it class declaration? Is it call syntax with substituted generic types?
      • If we want to have a premade format for this scenario, we should be very precise as to what is supported and what is not.

    Conclusion: Needs work. Exposing a UsePlusForNestedTypes option seems reasonable by itself. A format would then need to be very precise as to what it supports and where.

  6. added
    api-needs-work
    and removed
    api-ready-for-review
    on Apr 11, 2024
  7. tmat commented on Apr 12, 2024

    @tmat
    MemberAuthor

    My bad, I didn't notice that s_metadataDisplayFormat is only used by tests and is not the full implementation of fully qualified name.

    The intention was to allow the code written in the usage example - that is, given a ITypeSymbol return a fully qualified metadata name in the format that System.Reflection parses to load a type. See https://github.com/dotnet/runtime/blob/main/src/libraries/Common/src/System/Reflection/TypeNameParser.cs.

    This format is also used in ECMA-335 when serializing the value of System.Type for custom attribute blob. This is implemented by the compiler here: https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/PEWriter/TypeNameSerializer.cs

    Examples:

    typeof(X<int*[]>.Y<string>[,,]))
    
    X`1+Y`1[[System.Int32*[], System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]][,,]
    
    typeof(X<>.Y<>)
    
    X`1+Y`1
    
  8. removed
    untriaged
    on Apr 15, 2024
  9. modified the milestones: 17.11, 17.13 on Jul 23, 2024
  10. modified the milestones: 17.13, 17.13 P2 on Nov 13, 2024
  11. modified the milestones: 17.13 P2, 17.13 on Dec 2, 2024
  12. modified the milestones: 17.13, Backlog on Apr 1, 2025
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

    Area-CompilersConcept-APIFeature Requestapi-needs-work

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions