Extensions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ILRuntime.CLR.TypeSystem;
  6. using ILRuntime.CLR.Method;
  7. using ILRuntime.Other;
  8. using ILRuntime.Mono.Cecil;
  9. using ILRuntime.Runtime.Intepreter;
  10. using System.Reflection;
  11. namespace ILRuntime.CLR.Utils
  12. {
  13. public delegate TResult Func<T1, T2, T3, T4, T5, TResult>(T1 a1, T2 a2, T3 a3, T4 a4, T5 a5);
  14. public static class Extensions
  15. {
  16. public static List<IType> EmptyParamList = new List<IType>();
  17. public static List<IType> GetParamList(this MethodReference def, ILRuntime.Runtime.Enviorment.AppDomain appdomain, IType contextType, IMethod contextMethod, IType[] genericArguments)
  18. {
  19. if (def.HasParameters)
  20. {
  21. List<IType> param = new List<IType>();
  22. var dt = appdomain.GetType(def.DeclaringType, contextType, contextMethod);
  23. foreach (var i in def.Parameters)
  24. {
  25. IType t = null;
  26. t = appdomain.GetType(i.ParameterType, dt, null);
  27. if ((t == null && def.IsGenericInstance) || (t != null && t.HasGenericParameter))
  28. {
  29. GenericInstanceMethod gim = (GenericInstanceMethod)def;
  30. string name = i.ParameterType.IsByReference ? ((ByReferenceType)i.ParameterType).ElementType.FullName : i.ParameterType.FullName;
  31. for (int j = 0; j < gim.GenericArguments.Count; j++)
  32. {
  33. var gp = gim.ElementMethod.GenericParameters[j];
  34. var ga = gim.GenericArguments[j];
  35. if (name == gp.Name)
  36. {
  37. t = appdomain.GetType(ga, contextType, contextMethod);
  38. if (t == null && genericArguments != null)
  39. t = genericArguments[j];
  40. break;
  41. }
  42. else if (name.Contains(gp.Name))
  43. {
  44. t = appdomain.GetType(ga, contextType, contextMethod);
  45. if (t == null && genericArguments != null)
  46. t = genericArguments[j];
  47. if (name == gp.Name + "[]")
  48. {
  49. name = t.FullName + "[]";
  50. }
  51. else
  52. {
  53. /*name = name.Replace("<" + gp.Name + ">", "<" + ga.FullName + ">");
  54. name = name.Replace("<" + gp.Name + "[", "<" + ga.FullName + "[");
  55. name = name.Replace("<" + gp.Name + ",", "<" + ga.FullName + ",");
  56. name = name.Replace("," + gp.Name + ">", "," + ga.FullName + ">");
  57. name = name.Replace("," + gp.Name + "[", "," + ga.FullName + "[");
  58. name = name.Replace("," + gp.Name + ",", "," + ga.FullName + ",");
  59. name = name.Replace("," + gp.Name + "[", "," + ga.FullName + "[");*/
  60. name = ReplaceGenericArgument(name, gp.Name, t.FullName);
  61. }
  62. t = null;
  63. }
  64. }
  65. if(dt.GenericArguments != null)
  66. {
  67. foreach(var gp in dt.GenericArguments)
  68. {
  69. if (name.Contains(gp.Key))
  70. {
  71. name = ReplaceGenericArgument(name, gp.Key, gp.Value.FullName);
  72. }
  73. }
  74. }
  75. if (t == null)
  76. t = appdomain.GetType(name);
  77. if (t != null && i.ParameterType.IsByReference)
  78. t = t.MakeByRefType();
  79. }
  80. param.Add(t);
  81. }
  82. return param;
  83. }
  84. else
  85. return EmptyParamList;
  86. }
  87. static string ReplaceGenericArgument(string typename, string argumentName, string argumentType, bool isGA = false)
  88. {
  89. string baseType;
  90. StringBuilder sb = new StringBuilder();
  91. List<string> ga;
  92. bool isArray;
  93. byte rank;
  94. Runtime.Enviorment.AppDomain.ParseGenericType(typename, out baseType, out ga, out isArray, out rank);
  95. string baseTypeQualification = null;
  96. bool hasGA = ga != null && ga.Count > 0;
  97. if (baseType == argumentName)
  98. {
  99. bool isAssemblyQualified = argumentName.Contains('=') || argumentType.Contains('=');
  100. if (isGA && isAssemblyQualified)
  101. sb.Append('[');
  102. sb.Append(argumentType);
  103. if (isGA && isAssemblyQualified)
  104. sb.Append(']');
  105. }
  106. else
  107. {
  108. if (baseType.Contains("["))
  109. {
  110. baseType = ReplaceGenericArgument(baseType, argumentName, argumentType, isGA);
  111. }
  112. bool isAssemblyQualified = baseType.Contains('=');
  113. if (isGA && !hasGA && isAssemblyQualified)
  114. sb.Append('[');
  115. else if (isAssemblyQualified)
  116. {
  117. sb.Append('[');
  118. baseTypeQualification = baseType.Substring(baseType.IndexOf(','));
  119. baseType = baseType.Substring(0, baseType.IndexOf(','));
  120. }
  121. sb.Append(baseType);
  122. if (isGA && !hasGA && isAssemblyQualified)
  123. sb.Append(']');
  124. }
  125. if (hasGA)
  126. {
  127. sb.Append("[");
  128. bool isFirst = true;
  129. foreach (var i in ga)
  130. {
  131. if (isFirst)
  132. isFirst = false;
  133. else
  134. sb.Append(",");
  135. sb.Append(ReplaceGenericArgument(i, argumentName, argumentType, true));
  136. }
  137. sb.Append("]");
  138. }
  139. if (!string.IsNullOrEmpty(baseTypeQualification))
  140. {
  141. sb.Append(baseTypeQualification);
  142. sb.Append(']');
  143. }
  144. if (isArray)
  145. {
  146. sb.Append("[");
  147. for (int i = 0; i < rank - 1; i++)
  148. sb.Append(",");
  149. sb.Append("]");
  150. }
  151. return sb.ToString();
  152. }
  153. [Flags]
  154. public enum TypeFlags
  155. {
  156. Default = 0,
  157. IsPrimitive = 0x1,
  158. IsByRef = 0x2,
  159. IsEnum = 0x4,
  160. IsDelegate = 0x8,
  161. IsValueType = 0x10,
  162. }
  163. private static readonly Dictionary<Type, TypeFlags> typeFlags = new Dictionary<Type, TypeFlags>(new ByReferenceKeyComparer<Type>());
  164. public static bool FastIsEnum(this Type pt)
  165. {
  166. return (pt.GetTypeFlags() & TypeFlags.IsEnum) != 0;
  167. }
  168. public static bool FastIsByRef(this Type pt)
  169. {
  170. return (pt.GetTypeFlags() & TypeFlags.IsByRef) != 0;
  171. }
  172. public static bool FastIsPrimitive(this Type pt)
  173. {
  174. return (pt.GetTypeFlags() & TypeFlags.IsPrimitive) != 0;
  175. }
  176. public static bool FastIsValueType(this Type pt)
  177. {
  178. return (pt.GetTypeFlags() & TypeFlags.IsValueType) != 0;
  179. }
  180. public static TypeFlags GetTypeFlagsRecursive(this Type pt)
  181. {
  182. var res = GetTypeFlags(pt);
  183. if ((res & TypeFlags.IsByRef) == TypeFlags.IsByRef)
  184. res = GetTypeFlagsRecursive(pt.GetElementType());
  185. return res;
  186. }
  187. public static TypeFlags GetTypeFlags(this Type pt)
  188. {
  189. var result = TypeFlags.Default;
  190. if (!typeFlags.TryGetValue(pt, out result))
  191. {
  192. if (pt.IsPrimitive)
  193. {
  194. result |= TypeFlags.IsPrimitive;
  195. }
  196. if (pt == typeof(Delegate) || pt.IsSubclassOf(typeof(Delegate)))
  197. {
  198. result |= TypeFlags.IsDelegate;
  199. }
  200. if (pt.IsByRef)
  201. {
  202. result |= TypeFlags.IsByRef;
  203. }
  204. if (pt.IsEnum)
  205. {
  206. result |= TypeFlags.IsEnum;
  207. }
  208. if (pt.IsValueType)
  209. {
  210. result |= TypeFlags.IsValueType;
  211. }
  212. typeFlags[pt] = result;
  213. }
  214. return result;
  215. }
  216. public static object CheckCLRTypes(this Type pt, object obj)
  217. {
  218. var typeFlags = GetTypeFlags(pt);
  219. return CheckCLRTypes(pt, obj, typeFlags);
  220. }
  221. public static object CheckCLRTypes(this Type pt, object obj, TypeFlags typeFlags)
  222. {
  223. if (obj == null)
  224. return null;
  225. if ((typeFlags & TypeFlags.IsPrimitive) != 0)
  226. {
  227. if (pt == typeof(int)) return obj;
  228. if (pt == typeof(bool) && !(obj is bool))
  229. {
  230. obj = (int)obj == 1;
  231. }
  232. else if (pt == typeof(byte) && !(obj is byte))
  233. obj = (byte)(int)obj;
  234. else if (pt == typeof(short) && !(obj is short))
  235. obj = (short)(int)obj;
  236. else if (pt == typeof(char) && !(obj is char))
  237. obj = (char)(int)obj;
  238. else if (pt == typeof(ushort) && !(obj is ushort))
  239. obj = (ushort)(int)obj;
  240. else if (pt == typeof(uint) && !(obj is uint))
  241. obj = (uint)(int)obj;
  242. else if (pt == typeof(sbyte) && !(obj is sbyte))
  243. obj = (sbyte)(int)obj;
  244. else if (pt == typeof(ulong) && !(obj is ulong))
  245. {
  246. obj = (ulong)(long)obj;
  247. }
  248. }
  249. else if (obj is ILRuntime.Reflection.ILRuntimeWrapperType)
  250. {
  251. obj = ((ILRuntime.Reflection.ILRuntimeWrapperType)obj).RealType;
  252. }
  253. else if ((typeFlags & TypeFlags.IsDelegate) != 0)
  254. {
  255. if (obj is Delegate)
  256. return obj;
  257. if (pt == typeof(Delegate))
  258. return ((IDelegateAdapter)obj).Delegate;
  259. return ((IDelegateAdapter)obj).GetConvertor(pt);
  260. }
  261. else if ((typeFlags & TypeFlags.IsByRef) != 0)
  262. {
  263. return CheckCLRTypes(pt.GetElementType(), obj);
  264. }
  265. else if ((typeFlags & TypeFlags.IsEnum) != 0)
  266. {
  267. return Enum.ToObject(pt, obj);
  268. }
  269. else if (obj is ILTypeInstance)
  270. {
  271. var adapter = obj as IDelegateAdapter;
  272. if (adapter != null && pt != typeof(ILTypeInstance))
  273. {
  274. return adapter.Delegate;
  275. }
  276. if (!(obj is ILEnumTypeInstance))
  277. {
  278. var ins = (ILTypeInstance)obj;
  279. /*if (ins.IsValueType)
  280. ins = ins.Clone();*/
  281. return ins.CLRInstance;
  282. }
  283. }
  284. return obj;
  285. }
  286. public static bool CheckMethodParams(this MethodInfo m, Type[] args)
  287. {
  288. var arr = m.GetParameters();
  289. if (arr.Length != args.Length) return false;
  290. for (var i = 0; i < args.Length; i++)
  291. {
  292. var t1 = arr[i].ParameterType;
  293. var t2 = args[i];
  294. if (t1 != t2 || t1.IsByRef != t2.IsByRef)
  295. return false;
  296. }
  297. return true;
  298. }
  299. public static bool CheckMethodParams(this MethodInfo m, ParameterInfo[] args)
  300. {
  301. var arr = m.GetParameters();
  302. if (arr.Length != args.Length) return false;
  303. for (var i = 0; i < args.Length; i++)
  304. {
  305. var t1 = arr[i].ParameterType;
  306. var t2 = args[i].ParameterType;
  307. if (t1 != t2 || t1.IsByRef != t2.IsByRef)
  308. return false;
  309. }
  310. return true;
  311. }
  312. }
  313. }