Skip to content

add TypeName.CreateSimpleTypeName - #136275

Merged
jkotas merged 4 commits into
dotnet:mainfrom
lagudafuadtosin:api-102263
Nov 3, 2026
Merged

jkotas merged 4 commits into
dotnet:mainfrom
lagudafuadtosin:api-102263

Conversation

@lagudafuadtosin

Copy link
Copy Markdown
Contributor

fixes #102263

adds the remaining approved API. the name is escaped, nested names are joined to the declaring type with +. tests added.

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 22, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection-metadata
See info in area-owners.md if you want to be subscribed.

@lagudafuadtosin

lagudafuadtosin commented Oct 22, 2026 via email •

Copy link
Copy Markdown
Contributor Author

@lagudafuadtosin

lagudafuadtosin commented Oct 22, 2026 via email •

Copy link
Copy Markdown
Contributor Author

@jkotas

jkotas commented Oct 23, 2026

Copy link
Copy Markdown
Member

I do not see the problem. Can you elaborate?

@lagudafuadtosin

lagudafuadtosin commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor Author

I do not see the problem. Can you elaborate?

Sorry, I should have shown it, you see when the declaring type comes from the parser it knows where its name ends, so nothing goes wrong. But when it is made with CreateSimpleTypeName, it never went through the parser, so that length is -1. Then Name cuts the nested type's full name at position 0 and you get the whole thing back instead of just the nested name. That line rebuilds the declaring type with the right length so the cut lands after the +.

var outer = TypeName.CreateSimpleTypeName("Outer");
var inner = TypeName.CreateSimpleTypeName("Inner", declaringType: outer);
inner.Name // "Outer+Inner" without the line, "Inner" with it

Honestly, I didn't think of this until now, Name could use _declaringType.FullName.Length instead, and this line can go, should I do that instead?

@jkotas

jkotas commented Oct 23, 2026

Copy link
Copy Markdown
Member

For performance, it would be best to as lazy as possible with any allocations. The current API implementation in the PR is potentially going to allocate two TypeName instances and two full name strings for nested types.

Instead, it can just allocate TypeName instance, set _name, _declaringType and _assemblyName. Allocation of _fullname strings can happen lazily only once the user calls FullName property.

@lagudafuadtosin

Copy link
Copy Markdown
Contributor Author

For performance, it would be best to as lazy as possible with any allocations. The current API implementation in the PR is potentially going to allocate two TypeName instances and two full name strings for nested types.

Instead, it can just allocate TypeName instance, set _name, _declaringType and _assemblyName. Allocation of _fullname strings can happen lazily only once the user calls FullName property.

Done

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human review should confirm API-approval provenance and build/test results for the new public factory and shared name-generation changes.

No open issues 🎉

What changed in this PR

Adds TypeName.CreateSimpleTypeName to construct simple and nested type names without reparsing concatenated strings, completing the API work described in #102263.

Changes:

  • Adds the public factory with metadata-name escaping and argument validation.
  • Updates lazy name generation to support factory-created nested types.
  • Adds tests for escaping, nesting, assembly handling, derived names, and invalid arguments.
File Description
src/​libraries/​System.Reflection.Metadata/​tests/​Metadata/​TypeNameTests.cs Tests factory behavior and validation.
src/​libraries/​System.Reflection.Metadata/​src/​System/​Reflection/​Metadata/​TypeNameParserHelpers.cs Adds metadata-name escaping.
src/​libraries/​System.Reflection.Metadata/​src/​System/​Reflection/​Metadata/​TypeName.cs Implements the factory and nested-name support.
src/​libraries/​System.Reflection.Metadata/​src/​Resources/​Strings.resx Adds an error message for conflicting assembly arguments.
src/​libraries/​System.Reflection.Metadata/​ref/​System.Reflection.Metadata.cs Exposes the factory in the reference assembly.

Review effort: Balanced

@jkotas
jkotas merged commit f5c1fe1 into dotnet:main Nov 3, 2026
86 of 88 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Reflection.Metadata community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Creating new TypeName without reparsing

3 participants