PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/NUnit/core/TypeHelper.cs

#
C# | 209 lines | 165 code | 38 blank | 6 comment | 99 complexity | f74510fdcbb360355e070464b58658aa MD5 | raw file
Possible License(s): GPL-2.0
  1. // ****************************************************************
  2. // Copyright 2008, Charlie Poole
  3. // This is free software licensed under the NUnit license. You may
  4. // obtain a copy of the license at http://nunit.org.
  5. // ****************************************************************
  6. using System;
  7. using System.Reflection;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. namespace NUnit.Core
  11. {
  12. public class TypeHelper
  13. {
  14. public static string GetDisplayName(Type type)
  15. {
  16. #if NET_2_0
  17. if (type.IsGenericParameter)
  18. return type.Name;
  19. if (type.IsGenericType)
  20. {
  21. string name = type.FullName;
  22. int index = name.IndexOf("[[");
  23. if (index > 0)
  24. {
  25. int index2 = name.LastIndexOf("]]");
  26. if (index2 > index)
  27. name = name.Substring(0, index) + name.Substring(index2 + 2);
  28. }
  29. index = name.LastIndexOf('.');
  30. if (index >= 0) name = name.Substring(index + 1);
  31. index = name.IndexOf('`');
  32. while (index >= 0)
  33. {
  34. int index2 = name.IndexOf('+', index);
  35. if (index2 >= 0)
  36. name = name.Substring(0, index) + name.Substring(index2);
  37. else
  38. name = name.Substring(0, index);
  39. index = name.IndexOf('`');
  40. }
  41. StringBuilder sb = new StringBuilder(name);
  42. sb.Append("<");
  43. int cnt = 0;
  44. foreach (Type t in type.GetGenericArguments())
  45. {
  46. if (cnt++ > 0) sb.Append(",");
  47. sb.Append(GetDisplayName(t));
  48. }
  49. sb.Append(">");
  50. return sb.ToString();
  51. }
  52. #endif
  53. int lastdot = type.FullName.LastIndexOf('.');
  54. return lastdot >= 0
  55. ? type.FullName.Substring(lastdot + 1)
  56. : type.FullName;
  57. }
  58. public static string GetDisplayName(Type type, object[] arglist)
  59. {
  60. string baseName = GetDisplayName(type);
  61. if (arglist == null || arglist.Length == 0)
  62. return baseName;
  63. StringBuilder sb = new StringBuilder( baseName );
  64. sb.Append("(");
  65. for (int i = 0; i < arglist.Length; i++)
  66. {
  67. if (i > 0) sb.Append(",");
  68. object arg = arglist[i];
  69. string display = arg == null ? "null" : arg.ToString();
  70. if (arg is double || arg is float)
  71. {
  72. if (display.IndexOf('.') == -1)
  73. display += ".0";
  74. display += arg is double ? "d" : "f";
  75. }
  76. else if (arg is decimal) display += "m";
  77. else if (arg is long) display += "L";
  78. else if (arg is ulong) display += "UL";
  79. else if (arg is string) display = "\"" + display + "\"";
  80. sb.Append(display);
  81. }
  82. sb.Append(")");
  83. return sb.ToString();
  84. }
  85. public static Type BestCommonType(Type type1, Type type2)
  86. {
  87. if (type1 == type2) return type1;
  88. if (type1 == null) return type2;
  89. if (type2 == null) return type1;
  90. if (TypeHelper.IsNumeric(type1) && TypeHelper.IsNumeric(type2))
  91. {
  92. if (type1 == typeof(double)) return type1;
  93. if (type2 == typeof(double)) return type2;
  94. if (type1 == typeof(float)) return type1;
  95. if (type2 == typeof(float)) return type2;
  96. if (type1 == typeof(decimal)) return type1;
  97. if (type2 == typeof(decimal)) return type2;
  98. if (type1 == typeof(UInt64)) return type1;
  99. if (type2 == typeof(UInt64)) return type2;
  100. if (type1 == typeof(Int64)) return type1;
  101. if (type2 == typeof(Int64)) return type2;
  102. if (type1 == typeof(UInt32)) return type1;
  103. if (type2 == typeof(UInt32)) return type2;
  104. if (type1 == typeof(Int32)) return type1;
  105. if (type2 == typeof(Int32)) return type2;
  106. if (type1 == typeof(UInt16)) return type1;
  107. if (type2 == typeof(UInt16)) return type2;
  108. if (type1 == typeof(Int16)) return type1;
  109. if (type2 == typeof(Int16)) return type2;
  110. if (type1 == typeof(byte)) return type1;
  111. if (type2 == typeof(byte)) return type2;
  112. if (type1 == typeof(sbyte)) return type1;
  113. if (type2 == typeof(sbyte)) return type2;
  114. }
  115. return type1;
  116. }
  117. public static bool IsNumeric(Type type)
  118. {
  119. return type == typeof(double) ||
  120. type == typeof(float) ||
  121. type == typeof(decimal) ||
  122. type == typeof(Int64) ||
  123. type == typeof(Int32) ||
  124. type == typeof(Int16) ||
  125. type == typeof(UInt64) ||
  126. type == typeof(UInt32) ||
  127. type == typeof(UInt16) ||
  128. type == typeof(byte) ||
  129. type == typeof(sbyte);
  130. }
  131. #if NET_2_0
  132. public static Type MakeGenericType(Type type, Type[] typeArgs)
  133. {
  134. // TODO: Add error handling
  135. return type.MakeGenericType(typeArgs);
  136. }
  137. public static bool CanDeduceTypeArgsFromArgs(Type type, object[] arglist, ref Type[] typeArgsOut)
  138. {
  139. Type[] typeParameters = type.GetGenericArguments();
  140. foreach (ConstructorInfo ctor in type.GetConstructors())
  141. {
  142. ParameterInfo[] parameters = ctor.GetParameters();
  143. if (parameters.Length != arglist.Length)
  144. continue;
  145. Type[] typeArgs = new Type[typeParameters.Length];
  146. for (int i = 0; i < typeArgs.Length; i++)
  147. {
  148. for (int j = 0; j < arglist.Length; j++)
  149. {
  150. if (parameters[j].ParameterType.Equals(typeParameters[i]))
  151. typeArgs[i] = TypeHelper.BestCommonType(
  152. typeArgs[i],
  153. arglist[j].GetType());
  154. }
  155. if (typeArgs[i] == null)
  156. {
  157. typeArgs = null;
  158. break;
  159. }
  160. }
  161. if (typeArgs != null)
  162. {
  163. typeArgsOut = typeArgs;
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. #endif
  170. }
  171. }