2using System.Collections.Generic;
7 public static class TypeNameExtensions
9 public static string GetSourceCodeRepresentation(
this Type type)
13 return GetSourceCodeRepresentationInt(type,
new List<Type>());
21 private static string GetSourceCodeRepresentationInt(Type type, List<Type> travesed)
24 if (travesed.Count > 20)
throw new ArgumentException();
28 var prefixName =
string.Empty;
29 if (type.DeclaringType !=
null)
31 if (!travesed.Contains(type.DeclaringType))
32 prefixName = GetSourceCodeRepresentationInt(type.DeclaringType, travesed) +
".";
34 else if (!
string.IsNullOrEmpty(type.Namespace))
35 prefixName = type.Namespace +
".";
37 if (type.IsGenericType)
40 var genargNames = type.GetGenericArguments().Select(type1 => GetSourceCodeRepresentationInt(type1,
new List<Type>(Enumerable.Repeat<Type>(
null, travesed.Count))));
41 var idx = type.Name.IndexOf(
'`');
42 var
typename = idx > 0 ? type.Name.Substring(0, idx) : type.Name;
43 return string.Format(
"{0}{1}<{2}>", prefixName,
typename,
string.Join(
", ", genargNames.ToArray()));
48 return string.Format(
"{0}[{1}]", GetSourceCodeRepresentation(type.GetElementType()),
49 new string(Enumerable.Repeat(
',', type.GetArrayRank() - 1).ToArray()));
52 return string.Format(
"{0}{1}", prefixName, type.Name);