All Projects → stakx → TypeNameFormatter

stakx / TypeNameFormatter

Licence: MIT license
A small .NET library for formatting type names à la C#.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to TypeNameFormatter

Godot Gdscript Toolkit
Independent set of GDScript tools - parser, linter and formatter
Stars: ✭ 214 (+723.08%)
Mutual labels:  formatter
Typescript Express Starter
🚀 TypeScript Express Starter
Stars: ✭ 238 (+815.38%)
Mutual labels:  formatter
piranha.core.templates
Project templates for Piranha.Core
Stars: ✭ 21 (-19.23%)
Mutual labels:  dotnet-standard
Pp sql
Rails ActiveRecord SQL queries log beautifier
Stars: ✭ 223 (+757.69%)
Mutual labels:  formatter
Webpack Messages
Beautifully format Webpack messages throughout your bundle lifecycle(s)!
Stars: ✭ 238 (+815.38%)
Mutual labels:  formatter
Formatting
Type-safe, functional string formatting in Swift.
Stars: ✭ 248 (+853.85%)
Mutual labels:  formatter
Pronto
Quick automated code review of your changes
Stars: ✭ 2,450 (+9323.08%)
Mutual labels:  formatter
blackbricks
Black for Databricks notebooks
Stars: ✭ 40 (+53.85%)
Mutual labels:  formatter
Prettyhtml
💅 The formatter for the modern web https://prettyhtml.netlify.com/
Stars: ✭ 241 (+826.92%)
Mutual labels:  formatter
Sublime-uroboroSQL-formatter
Beautiful SQL Formatter for Sublime Text 3
Stars: ✭ 25 (-3.85%)
Mutual labels:  formatter
Nginx Config Formatter
nginx config file formatter/beautifier written in Python.
Stars: ✭ 222 (+753.85%)
Mutual labels:  formatter
Stringformatter
Simple Text Formetter (Credit Card Number, Phone Number, Serial Number etc.) Can be used in all text inputs according to the format pattern. If desired, large minor character restrictions can be made in the format pattern.
Stars: ✭ 231 (+788.46%)
Mutual labels:  formatter
Mbeautifier
MBeautifier is a MATLAB source code formatter, beautifier. It can be used directly in the MATLAB Editor and it is configurable.
Stars: ✭ 248 (+853.85%)
Mutual labels:  formatter
Juliaformatter.jl
An opinionated code formatter for Julia. Plot twist - the opinion is your own.
Stars: ✭ 217 (+734.62%)
Mutual labels:  formatter
idea-uroborosql-formatter
Beautiful SQL Formatter for IntelliJ Platform
Stars: ✭ 18 (-30.77%)
Mutual labels:  formatter
Powershell Beautifier
A whitespace reformatter and code cleaner for Windows PowerShell and PowerShell Core
Stars: ✭ 213 (+719.23%)
Mutual labels:  formatter
Luaformatter
Code formatter for Lua
Stars: ✭ 244 (+838.46%)
Mutual labels:  formatter
fixjson
JSON Fixer for Humans using (relaxed) JSON5
Stars: ✭ 96 (+269.23%)
Mutual labels:  formatter
NETCoreSync
NETCoreSync is a database synchronization framework where each client's local offline database (on each client's multiple devices) can be synchronized on-demand via network into a single centralized database hosted on a server. Data which are stored locally within each device of a single client can all be synchronized after each device have succ…
Stars: ✭ 71 (+173.08%)
Mutual labels:  dotnet-standard
Best Of Python Dev
🏆 A ranked list of awesome python developer tools and libraries. Updated weekly.
Stars: ✭ 243 (+834.62%)
Mutual labels:  formatter

TypeNameFormatter

TypeNameFormatter is a small .NET library for formatting type names à la C#.

NuGet badge AppVeyor AppVeyor tests Codecov

What is this good for?

Have you ever stumbled over the cryptic formatting of Type objects?

var someType = typeof(IEnumerable<int[]>);

Console.WriteLine(someType);
// => System.Collections.Generic.IEnumerable`1[System.Int32[]]

If you'd rather see something that looks more like a C# type name, then this library might be for you:

using TypeNameFormatter;

var someType = typeof(IEnumerable<int[]>);

Console.WriteLine(someType.GetFormattedName());
// => IEnumerable<int[]>

Formatting any Type involves more special cases than you might expect (such as generic types, nested types, multi-dimensional and jagged arrays, by-reference and pointer types). This library deals with all of those, so that you don't have to.

How do I use it?

By importing the TypeNameFormatter namespace, the following extension methods become available:

  • stringBuilder.AppendFormattedName(Type type, [TypeNameFormatOptions options]):
    Appends a C#-formatted type name to the given StringBuilder.

  • type.GetFormattedName([TypeNameFormatOptions options]):
    Returns a C#-formatted type name as a string. (This is a convenience method that does exactly the same as the above, using a throw-away StringBuilder.)

Both methods allow you to specify any combination of the following TypeNameFormatOptions flags:

  • Namespaces:
    Namespaces should be included. (For example, System.Action instead of Action.)

  • NoAnonymousTypes: Anonymous types should not have their "display class" name transformed to a more legible syntax. (For example, <>f__AnonymousType5<string, int> instead of {string Name, int Count}.)

  • NoGenericParameterNames:
    Parameter names of an open generic type should be omitted. (For example, IEnumerable<> instead of IEnumerable<T>. Note that this setting does not affect closed generic types; their arguments are always included.)

  • NoKeywords:
    Primitive types should not be mapped to their corresponding C# language keywords. (For example, Int32 instead of int.)

  • NoNullableQuestionMark: Nullable types should not be formatted using C# question mark syntax. (For example, Nullable<int> instead of int?.)

  • NoTuple: Value tuple types should not be formatted using C# tuple syntax. (For example, ValueTuple<bool, int> instead of (bool, int).)

But it doesn't format <some type> correctly!

If you think you've found a bug, please raise an issue so it can be looked into. (Make sure to mention the type that doesn't get formatted as expected.)

Alternatives

  • If you're targeting the .NET Framework, you can use good old System.CodeDom (which isn't particularly fast, however):

    using Microsoft.CSharp;
    using System.CodeDom;
    
    static string GetFormattedName(this Type type)
    {
        using (var provider = new CSharpCodeProvider())
        {
            var typeReference = new CodeTypeReference(type);
            return provider.GetTypeOutput(typeReference);
        }
    }
  • You could perhaps use Microsoft's .NET Compiler Platform (Roslyn), but that is a large library that can do much more than is needed.

Advanced usage

Configuration knobs for the source code distribution

The TypeNameFormatter.Sources NuGet package comes with a few MSBuild properties that you can set inside your project file (inside a <PropertyGroup>):

  • <TypeNameFormatterInternal>:
    This property determines the visibility of the types provided by TypeNameFormatter:

    • If set to True (the default), they are declared internal.
    • If set to False, they are declared public.
  • <TypeNameFormatterProjectNodeName>:
    This property determines the name under which TypeNameFormatter's single .cs file will appear in e.g. Visual Studio's Solution Explorer:

    • If set to TypeNameFormatter.cs (the default), a hidden linked file by that name will be added to your project's root.
    • If set to any other relative file path, a visible linked file will be added to your project.

For example:

<Project …>
  …
  <PropertyGroup>
    <!-- Make TypeNameFormatter's types `public` instead of `internal`: -->
    <TypeNameFormatterInternal>False<TypeNameFormatterInternal>

    <!-- Make a linked file `TypeNameFormatter.cs` show up in Solution Explorer
         under a folder node named `Utilities`: -->
    <TypeNameFormatterProjectNodeName>Utilities\TypeNameFormatter.cs</TypeNameFormatterProjectNodeName>
  </PropertyGroup>
  …
</Project>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].