Extensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ILRuntime.Runtime.Stack;
  6. namespace ILRuntime.Runtime
  7. {
  8. public static class Extensions
  9. {
  10. public static void GetClassName(this Type type, out string clsName, out string realClsName, out bool isByRef, bool simpleClassName = false)
  11. {
  12. isByRef = type.IsByRef;
  13. int arrayRank = 1;
  14. bool isArray = type.IsArray;
  15. if (isByRef)
  16. {
  17. type = type.GetElementType();
  18. }
  19. if (isArray)
  20. {
  21. arrayRank = type.GetArrayRank();
  22. type = type.GetElementType();
  23. if (type.IsArray)
  24. {
  25. type.GetClassName(out clsName, out realClsName, out isByRef, simpleClassName);
  26. clsName += "_Array";
  27. if (!simpleClassName)
  28. clsName += "_Binding";
  29. if (arrayRank > 1)
  30. clsName += arrayRank;
  31. if (arrayRank <= 1)
  32. realClsName += "[]";
  33. else
  34. {
  35. StringBuilder sb = new StringBuilder();
  36. sb.Append(realClsName);
  37. sb.Append('[');
  38. for (int i = 0; i < arrayRank - 1; i++)
  39. {
  40. sb.Append(',');
  41. }
  42. sb.Append(']');
  43. realClsName = sb.ToString();
  44. }
  45. return;
  46. }
  47. }
  48. string realNamespace = null;
  49. bool isNestedGeneric = false;
  50. if (type.IsNested)
  51. {
  52. string bClsName, bRealClsName;
  53. bool tmp;
  54. var rt = type.ReflectedType;
  55. if(rt.IsGenericType && rt.IsGenericTypeDefinition)
  56. {
  57. if (type.IsGenericType)
  58. {
  59. rt = rt.MakeGenericType(type.GetGenericArguments());
  60. isNestedGeneric = true;
  61. }
  62. }
  63. GetClassName(rt, out bClsName, out bRealClsName, out tmp);
  64. clsName = bClsName + "_";
  65. realNamespace = bRealClsName + ".";
  66. }
  67. else
  68. {
  69. clsName = simpleClassName ? "" : (!string.IsNullOrEmpty(type.Namespace) ? type.Namespace.Replace(".", "_") + "_" : "");
  70. realNamespace = !string.IsNullOrEmpty(type.Namespace) ? type.Namespace + "." : "global::";
  71. }
  72. clsName = clsName + type.Name.Replace(".", "_").Replace("`", "_").Replace("<", "_").Replace(">", "_");
  73. bool isGeneric = false;
  74. string ga = null;
  75. if (type.IsGenericType && !isNestedGeneric)
  76. {
  77. isGeneric = true;
  78. clsName += "_";
  79. ga = "<";
  80. var args = type.GetGenericArguments();
  81. bool first = true;
  82. foreach (var j in args)
  83. {
  84. if (first)
  85. first = false;
  86. else
  87. {
  88. clsName += "_";
  89. ga += ", ";
  90. }
  91. string a, b;
  92. bool tmp;
  93. GetClassName(j, out a, out b, out tmp, true);
  94. clsName += a;
  95. ga += b;
  96. }
  97. ga += ">";
  98. }
  99. if (isArray)
  100. {
  101. clsName += "_Array";
  102. if (arrayRank > 1)
  103. clsName += arrayRank;
  104. }
  105. if (!simpleClassName)
  106. clsName += "_Binding";
  107. realClsName = realNamespace;
  108. if (isGeneric)
  109. {
  110. int idx = type.Name.IndexOf("`");
  111. if (idx > 0)
  112. {
  113. realClsName += type.Name.Substring(0, idx);
  114. realClsName += ga;
  115. }
  116. else
  117. realClsName += type.Name;
  118. }
  119. else
  120. realClsName += type.Name;
  121. if (isArray)
  122. {
  123. if (arrayRank <= 1)
  124. realClsName += "[]";
  125. else
  126. {
  127. StringBuilder sb = new StringBuilder();
  128. sb.Append(realClsName);
  129. sb.Append('[');
  130. for(int i=0;i<arrayRank - 1; i++)
  131. {
  132. sb.Append(',');
  133. }
  134. sb.Append(']');
  135. realClsName = sb.ToString();
  136. }
  137. }
  138. }
  139. public static int ToInt32(this object obj)
  140. {
  141. if (obj is int)
  142. return (int)obj;
  143. if (obj is float)
  144. return (int)(float)obj;
  145. if (obj is long)
  146. return (int)(long)obj;
  147. if (obj is short)
  148. return (int)(short)obj;
  149. if (obj is double)
  150. return (int)(double)obj;
  151. if (obj is byte)
  152. return (int)(byte)obj;
  153. if (obj is Intepreter.ILEnumTypeInstance)
  154. return (int)((Intepreter.ILEnumTypeInstance)obj)[0];
  155. if (obj is uint)
  156. return (int)(uint)obj;
  157. if (obj is ushort)
  158. return (int)(ushort)obj;
  159. if (obj is sbyte)
  160. return (int)(sbyte)obj;
  161. throw new InvalidCastException();
  162. }
  163. public static long ToInt64(this object obj)
  164. {
  165. if (obj is long)
  166. return (long)obj;
  167. if (obj is int)
  168. return (long)(int)obj;
  169. if (obj is float)
  170. return (long)(float)obj;
  171. if (obj is short)
  172. return (long)(short)obj;
  173. if (obj is double)
  174. return (long)(double)obj;
  175. if (obj is byte)
  176. return (long)(byte)obj;
  177. if (obj is uint)
  178. return (long)(uint)obj;
  179. if (obj is ushort)
  180. return (long)(ushort)obj;
  181. if (obj is sbyte)
  182. return (long)(sbyte)obj;
  183. throw new InvalidCastException();
  184. }
  185. public static short ToInt16(this object obj)
  186. {
  187. if (obj is short)
  188. return (short)obj;
  189. if (obj is long)
  190. return (short)(long)obj;
  191. if (obj is int)
  192. return (short)(int)obj;
  193. if (obj is float)
  194. return (short)(float)obj;
  195. if (obj is double)
  196. return (short)(double)obj;
  197. if (obj is byte)
  198. return (short)(byte)obj;
  199. if (obj is uint)
  200. return (short)(uint)obj;
  201. if (obj is ushort)
  202. return (short)(ushort)obj;
  203. if (obj is sbyte)
  204. return (short)(sbyte)obj;
  205. throw new InvalidCastException();
  206. }
  207. public static float ToFloat(this object obj)
  208. {
  209. if (obj is float)
  210. return (float)obj;
  211. if (obj is int)
  212. return (float)(int)obj;
  213. if (obj is long)
  214. return (float)(long)obj;
  215. if (obj is short)
  216. return (float)(short)obj;
  217. if (obj is double)
  218. return (float)(double)obj;
  219. if (obj is byte)
  220. return (float)(byte)obj;
  221. if (obj is uint)
  222. return (float)(uint)obj;
  223. if (obj is ushort)
  224. return (float)(ushort)obj;
  225. if (obj is sbyte)
  226. return (float)(sbyte)obj;
  227. throw new InvalidCastException();
  228. }
  229. public static double ToDouble(this object obj)
  230. {
  231. if (obj is double)
  232. return (double)obj;
  233. if (obj is float)
  234. return (float)obj;
  235. if (obj is int)
  236. return (double)(int)obj;
  237. if (obj is long)
  238. return (double)(long)obj;
  239. if (obj is short)
  240. return (double)(short)obj;
  241. if (obj is byte)
  242. return (double)(byte)obj;
  243. if (obj is uint)
  244. return (double)(uint)obj;
  245. if (obj is ushort)
  246. return (double)(ushort)obj;
  247. if (obj is sbyte)
  248. return (double)(sbyte)obj;
  249. throw new InvalidCastException();
  250. }
  251. public static Type GetActualType(this object value)
  252. {
  253. if (value is ILRuntime.Runtime.Enviorment.CrossBindingAdaptorType)
  254. return ((ILRuntime.Runtime.Enviorment.CrossBindingAdaptorType)value).ILInstance.Type.ReflectionType;
  255. if (value is ILRuntime.Runtime.Intepreter.ILTypeInstance)
  256. return ((ILRuntime.Runtime.Intepreter.ILTypeInstance)value).Type.ReflectionType;
  257. else
  258. return value.GetType();
  259. }
  260. public static bool MatchGenericParameters(this System.Reflection.MethodInfo m, Type[] genericArguments, Type returnType, params Type[] parameters)
  261. {
  262. var param = m.GetParameters();
  263. if (param.Length == parameters.Length)
  264. {
  265. var args = m.GetGenericArguments();
  266. if (args.MatchGenericParameters(m.ReturnType, returnType, genericArguments))
  267. {
  268. for (int i = 0; i < param.Length; i++)
  269. {
  270. if (!args.MatchGenericParameters(param[i].ParameterType, parameters[i], genericArguments))
  271. return false;
  272. }
  273. return true;
  274. }
  275. else
  276. return false;
  277. }
  278. else
  279. return false;
  280. }
  281. public static bool MatchGenericParameters(this Type[] args, Type type, Type q, Type[] genericArguments)
  282. {
  283. if (type.IsGenericParameter)
  284. {
  285. for (int i = 0; i < args.Length; i++)
  286. {
  287. if (args[i] == type)
  288. {
  289. return q == genericArguments[i];
  290. }
  291. }
  292. throw new NotSupportedException();
  293. }
  294. else
  295. {
  296. if (type.IsArray)
  297. {
  298. if (q.IsArray)
  299. {
  300. return MatchGenericParameters(args, type.GetElementType(), q.GetElementType(), genericArguments);
  301. }
  302. else
  303. return false;
  304. }
  305. else if (type.IsByRef)
  306. {
  307. if (q.IsByRef)
  308. {
  309. return MatchGenericParameters(args, type.GetElementType(), q.GetElementType(), genericArguments);
  310. }
  311. else
  312. return false;
  313. }
  314. else if (type.IsGenericType)
  315. {
  316. if (q.IsGenericType)
  317. {
  318. var t1 = type.GetGenericTypeDefinition();
  319. var t2 = type.GetGenericTypeDefinition();
  320. if (t1 == t2)
  321. {
  322. var argA = type.GetGenericArguments();
  323. var argB = q.GetGenericArguments();
  324. if (argA.Length == argB.Length)
  325. {
  326. for (int i = 0; i < argA.Length; i++)
  327. {
  328. if (!MatchGenericParameters(args, argA[i], argB[i], genericArguments))
  329. return false;
  330. }
  331. return true;
  332. }
  333. else
  334. return false;
  335. }
  336. else
  337. return false;
  338. }
  339. else
  340. return false;
  341. }
  342. else
  343. return type == q;
  344. }
  345. }
  346. }
  347. }