Core - Added GetDisplayName Type extension

Data models - Improved nullable value type handling
This commit is contained in:
SpoinkyNL committed 2020-10-16 00:01:08 +02:00
1 parent e6417585b5
commit 7b7b75be78
3 files changed
+53 -1

No files matched your search

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Humanizer;
namespace Artemis.Core
{
@@ -63,6 +64,25 @@ namespace Artemis.Core
|| value is decimal;
}
private static readonly Dictionary<Type, string> TypeKeywords = new Dictionary<Type, string>()
{
{typeof(bool), "bool"},
{typeof(byte), "byte"},
{typeof(sbyte), "sbyte"},
{typeof(char), "char"},
{typeof(decimal), "decimal"},
{typeof(double), "double"},
{typeof(float), "float"},
{typeof(int), "int"},
{typeof(uint), "uint"},
{typeof(long), "long"},
{typeof(ulong), "ulong"},
{typeof(short), "short"},
{typeof(ushort), "ushort"},
{typeof(object), "object"},
{typeof(string), "string"},
};
// From https://stackoverflow.com/a/2224421/5015269 but inverted and renamed to match similar framework methods
/// <summary>
/// Determines whether an instance of a specified type can be casted to a variable of the current type
@@ -127,5 +147,27 @@ namespace Artemis.Core
return enumerableType?.GenericTypeArguments[0];
}
/// <summary>
/// Determines a display name for the given type
/// </summary>
/// <param name="type">The type to determine the name for</param>
/// <param name="humanize">Whether or not to humanize the result, defaults to false</param>
/// <returns></returns>
public static string GetDisplayName(this Type type, bool humanize = false)
{
if (!type.IsGenericType)
{
string displayValue = TypeKeywords.TryGetValue(type, out string? keyword) ? keyword! : type.Name;
return humanize ? displayValue.Humanize() : displayValue;
}
Type genericTypeDefinition = type.GetGenericTypeDefinition();
if (genericTypeDefinition == typeof(Nullable<>))
return type.GenericTypeArguments[0].GetDisplayName(humanize) + "?";
string stripped = genericTypeDefinition.Name.Split('`')[0];
return $"{stripped}<{string.Join(", ", type.GenericTypeArguments.Select(t => t.GetDisplayName(humanize)))}>";
}
}
}