ILRuntimeMethodInfo.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. using System.Globalization;
  7. using ILRuntime.CLR.Method;
  8. using ILRuntime.CLR.Utils;
  9. using ILRuntime.Runtime.Intepreter;
  10. using ILRuntime.Runtime.Enviorment;
  11. namespace ILRuntime.Reflection
  12. {
  13. public class ILRuntimeMethodInfo : MethodInfo
  14. {
  15. ILMethod method;
  16. ILRuntimeParameterInfo[] parameters;
  17. Mono.Cecil.MethodDefinition definition;
  18. ILRuntime.Runtime.Enviorment.AppDomain appdomain;
  19. Attribute[] customAttributes;
  20. Type[] attributeTypes;
  21. public ILRuntimeMethodInfo(ILMethod m)
  22. {
  23. method = m;
  24. definition = m.Definition;
  25. appdomain = m.DeclearingType.AppDomain;
  26. parameters = new ILRuntimeParameterInfo[m.ParameterCount];
  27. for (int i = 0; i < m.ParameterCount; i++)
  28. {
  29. var pd = m.Definition.Parameters[i];
  30. parameters[i] = new ILRuntimeParameterInfo(pd, m.Parameters[i], this);
  31. }
  32. }
  33. void InitializeCustomAttribute()
  34. {
  35. customAttributes = new Attribute[definition.CustomAttributes.Count];
  36. attributeTypes = new Type[customAttributes.Length];
  37. for (int i = 0; i < definition.CustomAttributes.Count; i++)
  38. {
  39. var attribute = definition.CustomAttributes[i];
  40. var at = appdomain.GetType(attribute.AttributeType, null, null);
  41. try
  42. {
  43. Attribute ins = attribute.CreateInstance(at, appdomain) as Attribute;
  44. attributeTypes[i] = at.ReflectionType;
  45. customAttributes[i] = ins;
  46. }
  47. catch
  48. {
  49. attributeTypes[i] = typeof(Attribute);
  50. }
  51. }
  52. }
  53. internal ILMethod ILMethod { get { return method; } }
  54. public override MethodAttributes Attributes
  55. {
  56. get
  57. {
  58. MethodAttributes ma = MethodAttributes.Public;
  59. if (definition.IsPrivate)
  60. ma = MethodAttributes.Private;
  61. else if (definition.IsFamily)
  62. ma = MethodAttributes.Family;
  63. if (method.IsStatic)
  64. ma |= MethodAttributes.Static;
  65. if (method.IsVirtual)
  66. ma |= MethodAttributes.Virtual;
  67. return ma;
  68. }
  69. }
  70. public override Type DeclaringType
  71. {
  72. get
  73. {
  74. return method.DeclearingType.ReflectionType;
  75. }
  76. }
  77. public override RuntimeMethodHandle MethodHandle
  78. {
  79. get
  80. {
  81. throw new NotImplementedException();
  82. }
  83. }
  84. public override string Name
  85. {
  86. get
  87. {
  88. return method.Name;
  89. }
  90. }
  91. public override Type ReflectedType
  92. {
  93. get
  94. {
  95. return method.DeclearingType.ReflectionType;
  96. }
  97. }
  98. public override ICustomAttributeProvider ReturnTypeCustomAttributes
  99. {
  100. get
  101. {
  102. throw new NotImplementedException();
  103. }
  104. }
  105. public override MethodInfo GetBaseDefinition()
  106. {
  107. return this;
  108. }
  109. public override object[] GetCustomAttributes(bool inherit)
  110. {
  111. if (customAttributes == null)
  112. InitializeCustomAttribute();
  113. return customAttributes;
  114. }
  115. public override object[] GetCustomAttributes(Type attributeType, bool inherit)
  116. {
  117. if (customAttributes == null)
  118. InitializeCustomAttribute();
  119. List<object> res = new List<object>();
  120. for (int i = 0; i < customAttributes.Length; i++)
  121. {
  122. if (attributeTypes[i].Equals(attributeType))
  123. res.Add(customAttributes[i]);
  124. }
  125. return res.ToArray();
  126. }
  127. public override MethodImplAttributes GetMethodImplementationFlags()
  128. {
  129. throw new NotImplementedException();
  130. }
  131. public override ParameterInfo[] GetParameters()
  132. {
  133. return parameters;
  134. }
  135. public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
  136. {
  137. if (method.HasThis)
  138. {
  139. var res = appdomain.Invoke(method, obj, parameters);
  140. return ReturnType.CheckCLRTypes(res);
  141. }
  142. else
  143. return appdomain.Invoke(method, null, parameters);
  144. }
  145. public override bool IsDefined(Type attributeType, bool inherit)
  146. {
  147. if (customAttributes == null)
  148. InitializeCustomAttribute();
  149. for (int i = 0; i < customAttributes.Length; i++)
  150. {
  151. if (attributeTypes[i] == attributeType)
  152. return true;
  153. }
  154. return false;
  155. }
  156. public override Type ReturnType
  157. {
  158. get
  159. {
  160. if (method.ReturnType != null)
  161. return method.ReturnType.ReflectionType;
  162. else
  163. return null;
  164. }
  165. }
  166. #if NET_4_6 || NET_STANDARD_2_0
  167. public override Delegate CreateDelegate(Type delegateType)
  168. {
  169. throw new NotSupportedException("please use CreateDelegate(Type delegateType, object target)");
  170. }
  171. private IDelegateAdapter iDelegate;
  172. public override Delegate CreateDelegate(Type delegateType, object target)
  173. {
  174. ILTypeInstance ilTypeInstance;
  175. if (target is ILTypeInstance)
  176. {
  177. ilTypeInstance = target as ILTypeInstance;
  178. }
  179. else if (target is CrossBindingAdaptorType adaptor)
  180. {
  181. ilTypeInstance = adaptor.ILInstance;
  182. }
  183. else
  184. {
  185. return null;
  186. }
  187. IDelegateAdapter del;
  188. if (iDelegate == null)
  189. {
  190. iDelegate = appdomain.DelegateManager.FindDelegateAdapter(ilTypeInstance, method, method);
  191. del = iDelegate;
  192. }
  193. else
  194. {
  195. del = iDelegate.Instantiate(appdomain, ilTypeInstance, iDelegate.Method);
  196. }
  197. return del.GetConvertor(delegateType);
  198. }
  199. #endif
  200. }
  201. }