/// <summary>Returns a string representation of <paramref name="type"/>.</summary> /// <param name="type">The type.</param> /// <param name="options">The type formatting options.</param> /// <param name="getNameFunc">The delegate used to get the unadorned, simple type name of <paramref name="type"/>.</param> /// <returns>A string representation of the <paramref name="type"/>.</returns> public static string GetParseableName(this Type type, TypeFormattingOptions options = null, Func <Type, string> getNameFunc = null) { options = options ?? TypeFormattingOptions.Default; // If a naming function has been specified, skip the cache. if (getNameFunc != null) { return(BuildParseableName()); } return(ParseableNameCache.GetOrAdd(Tuple.Create(type, options), _ => BuildParseableName())); string BuildParseableName() { var builder = new StringBuilder(); var typeInfo = type.GetTypeInfo(); GetParseableName( type, builder, new Queue <Type>( typeInfo.IsGenericTypeDefinition ? typeInfo.GetGenericArguments() : typeInfo.GenericTypeArguments), options, getNameFunc ?? (t => t.GetUnadornedTypeName() + options.NameSuffix)); return(builder.ToString()); } }
/// <summary>Returns a string representation of <paramref name="type"/>.</summary> /// <param name="type">The type.</param> /// <param name="options">The type formatting options.</param> /// <returns>A string representation of the <paramref name="type"/>.</returns> public static string GetParseableName(this Type type, TypeFormattingOptions options = null) { options = options ?? new TypeFormattingOptions(); return(ParseableNameCache.GetOrAdd( Tuple.Create(type, options), _ => { var builder = new StringBuilder(); var typeInfo = type.GetTypeInfo(); GetParseableName( type, builder, new Queue <Type>( typeInfo.IsGenericTypeDefinition ? typeInfo.GetGenericArguments() : typeInfo.GenericTypeArguments), options); return builder.ToString(); })); }
/// <summary>Returns a string representation of <paramref name="type"/>.</summary> /// <param name="type">The type.</param> /// <param name="builder">The <see cref="StringBuilder"/> to append results to.</param> /// <param name="typeArguments">The type arguments of <paramref name="type"/>.</param> /// <param name="options">The type formatting options.</param> /// <param name="getNameFunc">Delegate that returns name for a type.</param> private static void GetParseableName( Type type, StringBuilder builder, Queue <Type> typeArguments, TypeFormattingOptions options, Func <Type, string> getNameFunc) { var typeInfo = type.GetTypeInfo(); if (typeInfo.IsArray) { var elementType = typeInfo.GetElementType().GetParseableName(options); if (!string.IsNullOrWhiteSpace(elementType)) { builder.AppendFormat( "{0}[{1}]", elementType, string.Concat(Enumerable.Range(0, type.GetArrayRank() - 1).Select(_ => ','))); } return; } if (typeInfo.IsGenericParameter) { if (options.IncludeGenericTypeParameters) { builder.Append(type.GetUnadornedTypeName()); } return; } if (typeInfo.DeclaringType != null) { // This is not the root type. GetParseableName(typeInfo.DeclaringType, builder, typeArguments, options, t => t.GetUnadornedTypeName()); builder.Append(options.NestedTypeSeparator); } else if (!string.IsNullOrWhiteSpace(type.Namespace) && options.IncludeNamespace) { // This is the root type, so include the namespace. var namespaceName = type.Namespace; if (options.NestedTypeSeparator != '.') { namespaceName = namespaceName.Replace('.', options.NestedTypeSeparator); } if (options.IncludeGlobal) { builder.AppendFormat("global::"); } builder.AppendFormat("{0}{1}", namespaceName, options.NestedTypeSeparator); } if (type.IsConstructedGenericType) { // Get the unadorned name, the generic parameters, and add them together. var unadornedTypeName = getNameFunc(type); builder.Append(EscapeIdentifier(unadornedTypeName)); var generics = Enumerable.Range(0, Math.Min(typeInfo.GetGenericArguments().Count(), typeArguments.Count)) .Select(_ => typeArguments.Dequeue()) .ToList(); if (generics.Count > 0 && options.IncludeTypeParameters) { var genericParameters = string.Join( ",", generics.Select(generic => GetParseableName(generic, options))); builder.AppendFormat("<{0}>", genericParameters); } } else if (typeInfo.IsGenericTypeDefinition) { // Get the unadorned name, the generic parameters, and add them together. var unadornedTypeName = getNameFunc(type); builder.Append(EscapeIdentifier(unadornedTypeName)); var generics = Enumerable.Range(0, Math.Min(type.GetGenericArguments().Count(), typeArguments.Count)) .Select(_ => typeArguments.Dequeue()) .ToList(); if (generics.Count > 0 && options.IncludeTypeParameters) { var genericParameters = string.Join( ",", generics.Select(_ => options.IncludeGenericTypeParameters ? _.ToString() : string.Empty)); builder.AppendFormat("<{0}>", genericParameters); } } else { builder.Append(EscapeIdentifier(getNameFunc(type))); } }