Extensions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. bool isAssemblyQualified = baseType.Contains('=');
  109. if (isGA && !hasGA && isAssemblyQualified)
  110. sb.Append('[');
  111. else if (isAssemblyQualified)
  112. {
  113. sb.Append('[');
  114. baseTypeQualification = baseType.Substring(baseType.IndexOf(','));
  115. baseType = baseType.Substring(0, baseType.IndexOf(','));
  116. }
  117. sb.Append(baseType);
  118. if (isGA && !hasGA && isAssemblyQualified)
  119. sb.Append(']');
  120. }
  121. if (hasGA)
  122. {
  123. sb.Append("[");
  124. bool isFirst = true;
  125. foreach (var i in ga)
  126. {
  127. if (isFirst)
  128. isFirst = false;
  129. else
  130. sb.Append(",");
  131. sb.Append(ReplaceGenericArgument(i, argumentName, argumentType, true));
  132. }
  133. sb.Append("]");
  134. }
  135. if (!string.IsNullOrEmpty(baseTypeQualification))
  136. {
  137. sb.Append(baseTypeQualification);
  138. sb.Append(']');
  139. }
  140. if (isArray)
  141. {
  142. sb.Append("[");
  143. for (int i = 0; i < rank - 1; i++)
  144. sb.Append(",");
  145. sb.Append("]");
  146. }
  147. return sb.ToString();
  148. }
  149. [Flags]
  150. public enum TypeFlags
  151. {
  152. Default = 0,
  153. IsPrimitive = 0x1,
  154. IsByRef = 0x2,
  155. IsEnum = 0x4,
  156. IsDelegate = 0x8,
  157. IsValueType = 0x10,
  158. }
  159. private static readonly Dictionary<Type, TypeFlags> typeFlags = new Dictionary<Type, TypeFlags>(new ByReferenceKeyComparer<Type>());
  160. public static bool FastIsEnum(this Type pt)
  161. {
  162. return (pt.GetTypeFlags() & TypeFlags.IsEnum) != 0;
  163. }
  164. public static bool FastIsByRef(this Type pt)
  165. {
  166. return (pt.GetTypeFlags() & TypeFlags.IsByRef) != 0;
  167. }
  168. public static bool FastIsPrimitive(this Type pt)
  169. {
  170. return (pt.GetTypeFlags() & TypeFlags.IsPrimitive) != 0;
  171. }
  172. public static bool FastIsValueType(this Type pt)
  173. {
  174. return (pt.GetTypeFlags() & TypeFlags.IsValueType) != 0;
  175. }
  176. public static TypeFlags GetTypeFlags(this Type pt)
  177. {
  178. var result = TypeFlags.Default;
  179. if (!typeFlags.TryGetValue(pt, out result))
  180. {
  181. if (pt.IsPrimitive)
  182. {
  183. result |= TypeFlags.IsPrimitive;
  184. }
  185. if (pt == typeof(Delegate) || pt.IsSubclassOf(typeof(Delegate)))
  186. {
  187. result |= TypeFlags.IsDelegate;
  188. }
  189. if (pt.IsByRef)
  190. {
  191. result |= TypeFlags.IsByRef;
  192. }
  193. if (pt.IsEnum)
  194. {
  195. result |= TypeFlags.IsEnum;
  196. }
  197. if (pt.IsValueType)
  198. {
  199. result |= TypeFlags.IsValueType;
  200. }
  201. typeFlags[pt] = result;
  202. }
  203. return result;
  204. }
  205. public static object CheckCLRTypes(this Type pt, object obj)
  206. {
  207. if (obj == null)
  208. return null;
  209. var typeFlags = GetTypeFlags(pt);
  210. if ((typeFlags & TypeFlags.IsPrimitive) != 0)
  211. {
  212. if (pt == typeof(int)) return obj;
  213. if (pt == typeof(bool) && !(obj is bool))
  214. {
  215. obj = (int)obj == 1;
  216. }
  217. else if (pt == typeof(byte) && !(obj is byte))
  218. obj = (byte)(int)obj;
  219. else if (pt == typeof(short) && !(obj is short))
  220. obj = (short)(int)obj;
  221. else if (pt == typeof(char) && !(obj is char))
  222. obj = (char)(int)obj;
  223. else if (pt == typeof(ushort) && !(obj is ushort))
  224. obj = (ushort)(int)obj;
  225. else if (pt == typeof(uint) && !(obj is uint))
  226. obj = (uint)(int)obj;
  227. else if (pt == typeof(sbyte) && !(obj is sbyte))
  228. obj = (sbyte)(int)obj;
  229. else if (pt == typeof(ulong) && !(obj is ulong))
  230. {
  231. obj = (ulong)(long)obj;
  232. }
  233. }
  234. else if (obj is ILRuntime.Reflection.ILRuntimeWrapperType)
  235. {
  236. obj = ((ILRuntime.Reflection.ILRuntimeWrapperType)obj).RealType;
  237. }
  238. else if ((typeFlags & TypeFlags.IsDelegate) != 0)
  239. {
  240. if (obj is Delegate)
  241. return obj;
  242. if (pt == typeof(Delegate))
  243. return ((IDelegateAdapter)obj).Delegate;
  244. return ((IDelegateAdapter)obj).GetConvertor(pt);
  245. }
  246. else if ((typeFlags & TypeFlags.IsByRef) != 0)
  247. {
  248. return CheckCLRTypes(pt.GetElementType(), obj);
  249. }
  250. else if ((typeFlags & TypeFlags.IsEnum) != 0)
  251. {
  252. return Enum.ToObject(pt, obj);
  253. }
  254. else if (obj is ILTypeInstance)
  255. {
  256. var adapter = obj as IDelegateAdapter;
  257. if (adapter != null && pt != typeof(ILTypeInstance))
  258. {
  259. return adapter.Delegate;
  260. }
  261. if (!(obj is ILEnumTypeInstance))
  262. {
  263. var ins = (ILTypeInstance)obj;
  264. /*if (ins.IsValueType)
  265. ins = ins.Clone();*/
  266. return ins.CLRInstance;
  267. }
  268. }
  269. return obj;
  270. }
  271. public static bool CheckMethodParams(this MethodInfo m, Type[] args)
  272. {
  273. var arr = m.GetParameters();
  274. if (arr.Length != args.Length) return false;
  275. for (var i = 0; i < args.Length; i++)
  276. {
  277. var t1 = arr[i].ParameterType;
  278. var t2 = args[i];
  279. if (t1 != t2 || t1.IsByRef != t2.IsByRef)
  280. return false;
  281. }
  282. return true;
  283. }
  284. public static bool CheckMethodParams(this MethodInfo m, ParameterInfo[] args)
  285. {
  286. var arr = m.GetParameters();
  287. if (arr.Length != args.Length) return false;
  288. for (var i = 0; i < args.Length; i++)
  289. {
  290. var t1 = arr[i].ParameterType;
  291. var t2 = args[i].ParameterType;
  292. if (t1 != t2 || t1.IsByRef != t2.IsByRef)
  293. return false;
  294. }
  295. return true;
  296. }
  297. }
  298. }