diff --git a/ScriptCore/Extensions/TypeExtension.cs b/ScriptCore/Extensions/TypeExtension.cs index b8026c4..60b0b0a 100644 --- a/ScriptCore/Extensions/TypeExtension.cs +++ b/ScriptCore/Extensions/TypeExtension.cs @@ -41,7 +41,7 @@ public static class TypeExtension foreach (Assembly domainAssembly in AppDomain.CurrentDomain.GetAssemblies()) foreach (Type type in domainAssembly.GetTypes()) { - if (baseType.IsAssignableFrom(type) && !type.IsAbstract) yield return type; + if (!type.IsAbstract && baseType.IsAssignableFrom(type)) yield return type; } } @@ -158,4 +158,73 @@ public static class TypeExtension return null; } + + /// + /// Gets the simple name of the type. This is the name of the type without any generic arguments. + /// + /// The type whose name is to be simplified. + /// The simple name of the type. + public static string GetSimpleName(this Type type) + { + string uglyName = type.Name; + + int tickIndex = uglyName.IndexOf('`'); + + if (tickIndex < 0) + { + return uglyName; + } + + return uglyName.Substring(0, tickIndex); + } + + /// + /// Gets the pretty name of the type, including generic arguments. This prints the type name as it would be written in C# code. + /// If the type is not generic, the name is returned as is. + /// + /// The type whose name is to be written pretty. + /// The pretty name of the type. + public static string GetPrettyName(this Type type) + { + if (!type.IsGenericType) + { + return type.Name; + } + + string uglyName = type.Name; + + int tickIndex = uglyName.IndexOf('`'); + + // No tick in generic name? + if (tickIndex < 0) + { + return uglyName; + } + + StringBuilder nameBuilder = new StringBuilder(uglyName.Substring(0, tickIndex)); + + nameBuilder.Append('<'); + + Type[] genericParameters = type.GetGenericArguments(); + + bool first = true; + + foreach (Type genericParameter in genericParameters) + { + if (!first) + { + nameBuilder.Append(", "); + } + else + { + first = false; + } + + nameBuilder.Append(genericParameter.Name); + } + + nameBuilder.Append('>'); + + return nameBuilder.ToString(); + } }