Add GetFriendlyName and GetFullFriendlyName extension methods on Type - #171
Merged
Conversation
Renders a type the way a C# developer would write it, instead of the
raw CLR name:
- Common CLR primitives map to their C# keyword alias (int, string, etc.)
- Nullable value types render with a trailing '?'
- Arrays respect their rank (int[], int[,])
- Nested types use '.' instead of the CLR '+'
- Open generic type definitions use the generic parameter's own name
(List<T> instead of List<>)
- GetFullFriendlyName includes the namespace-qualified name, recursively
applied to generic type arguments
- Types without a friendly name (tuples, anonymous types) fall back to
a structural rendering, e.g. (int, string) and { Name, Age }
Closes #161
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Resolves conflicts with #159's type classifier extension methods (IsNullable, IsEnumerable, GetElementTypeOfEnumerable, IsDictionary, TryGetDictionaryTypes, IsAwaitable, IsTaskLike, IsNumeric, IsPrimitiveOrString) which landed alongside GetFriendlyName/ GetFullFriendlyName. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #161
Summary
Implements the approved API proposal from #161:
GetFriendlyName()andGetFullFriendlyName()extension methods onType, rendering a type the way a C# developer would write it instead of the raw CLR name.Behavior
Dictionary<string, List<int>>→Dictionary<string, List<int>>int?→int?(nullable value types use?)int[]/int[,]for arrays, respecting rankOuter.Innerfor nested types (+replaced with.)List<>(open generic type definition) →List<T>, using the generic parameter's own nameint,string,bool,double,float,decimal,long,short,byte,sbyte,uint,ulong,ushort,char,object,voidGetFullFriendlyName()behaves the same but uses the namespace-qualified name for the outer type and recursively for all generic type arguments, e.g.System.Collections.Generic.Dictionary<string, System.Collections.Generic.List<int>>(primitives are still rendered as C# keywords, since those have no namespace-qualified form)HasFriendlyName()returnsfalseget a structural fallback:ValueTuple/Tuple, perIsTuple()) render as(int, string), using the friendly names of their element types recursively{ Name, Age }, using the property names in declaration orderTests
Added
GetFriendlyNameandGetFullFriendlyNamenested test classes toTypeMetaDataExtensionsSpecs.cscovering simple types, primitive keyword aliases, nullable value types, single/multi-dimensional arrays, nested types, generics (single/multiple/nested type arguments), open generic type definitions, tuples, anonymous types, and the full-name variants.Validation
dotnet build src\Reflectify\Reflectify.csproj— succeeds for all four target frameworks (net47, net6.0, netstandard2.0, netstandard2.1)dotnet test tests\Reflectify.Specs\Reflectify.Specs.csproj --framework net8.0— 223/223 tests pass (net472 and net6.0 also pass; netcoreapp3.0 can't run locally due to a missing runtime, unrelated to this change)Docs
Updated the "How do I use it?" section of
README.mdto mention the new methods.Co-authored-by: Copilot App 223556219+Copilot@users.noreply.github.com