https://github.com/dadhi/csharptypeprinter
Prints System.Type object as a valid C# literal, e.g. typeof(A<X>.B<Y>.C) as a "A<X>.B<Y>.C"
https://github.com/dadhi/csharptypeprinter
automation code-generation csharp dotnet happiness pretty-print reflection to-string type
Last synced: 5 months ago
JSON representation
Prints System.Type object as a valid C# literal, e.g. typeof(A<X>.B<Y>.C) as a "A<X>.B<Y>.C"
- Host: GitHub
- URL: https://github.com/dadhi/csharptypeprinter
- Owner: dadhi
- License: mit
- Created: 2020-09-14T10:07:10.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-10-15T13:19:55.000Z (almost 5 years ago)
- Last Synced: 2025-04-19T10:28:07.202Z (6 months ago)
- Topics: automation, code-generation, csharp, dotnet, happiness, pretty-print, reflection, to-string, type
- Language: C#
- Homepage:
- Size: 25.4 KB
- Stars: 8
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CSharpTypePrinter
[](https://www.nuget.org/packages/CSharpTypePrinter)[](https://www.fuget.org/packages/CSharpTypePrinter/1.0.0/)
Targets **.NET Standard 2.0**
Prints a `System.Type` object as a valid C# literal, e.g. prints `typeof(A.B.C)` as a `"A.B.C"`
It happens that the code for this is the complex pile of details especially if we talk about nested generics.
So I wanted to automate it and get and the robust implementation. A similar code is used Today by three of my projects: [DryIoc](https://github.com/dadhi/DryIoc), [FastExpressionCompiler](https://github.com/dadhi/FastExpressionCompiler), [ImTools](https://github.com/dadhi/ImTools).
The library contains a single extension method:
```cs
public static class TypePrinter
{
public static string ToCSharpCode(this Type type,
bool stripNamespace = false,
Func printType = null,
bool printGenericTypeArgs = false)
{
//:-)
}
}
```The options include:
- `stripNamespace` self explanatory.
- `printType` function may configure the final result given the input type and the output string.
- `printGenericTypeArgs` if set to true will output open-generic type as `Blah` instead of `Blah<>`. The default value is false because of my own use-case of the type inside the `typeof()` where `typeof(Blah<>)` is the valid code and the `typeof(Blah)` is not.Happy coding!