ILRuntimeMethodInfo.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 (method.IsStatic)
  60. ma |= MethodAttributes.Static;
  61. return ma;
  62. }
  63. }
  64. public override Type DeclaringType
  65. {
  66. get
  67. {
  68. return method.DeclearingType.ReflectionType;
  69. }
  70. }
  71. public override RuntimeMethodHandle MethodHandle
  72. {
  73. get
  74. {
  75. throw new NotImplementedException();
  76. }
  77. }
  78. public override string Name
  79. {
  80. get
  81. {
  82. return method.Name;
  83. }
  84. }
  85. public override Type ReflectedType
  86. {
  87. get
  88. {
  89. return method.DeclearingType.ReflectionType;
  90. }
  91. }
  92. public override ICustomAttributeProvider ReturnTypeCustomAttributes
  93. {
  94. get
  95. {
  96. throw new NotImplementedException();
  97. }
  98. }
  99. public override MethodInfo GetBaseDefinition()
  100. {
  101. return this;
  102. }
  103. public override object[] GetCustomAttributes(bool inherit)
  104. {
  105. if (customAttributes == null)
  106. InitializeCustomAttribute();
  107. return customAttributes;
  108. }
  109. public override object[] GetCustomAttributes(Type attributeType, bool inherit)
  110. {
  111. if (customAttributes == null)
  112. InitializeCustomAttribute();
  113. List<object> res = new List<object>();
  114. for (int i = 0; i < customAttributes.Length; i++)
  115. {
  116. if (attributeTypes[i].Equals(attributeType))
  117. res.Add(customAttributes[i]);
  118. }
  119. return res.ToArray();
  120. }
  121. public override MethodImplAttributes GetMethodImplementationFlags()
  122. {
  123. throw new NotImplementedException();
  124. }
  125. public override ParameterInfo[] GetParameters()
  126. {
  127. return parameters;
  128. }
  129. public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
  130. {
  131. if (method.HasThis)
  132. {
  133. var res = appdomain.Invoke(method, obj, parameters);
  134. return ReturnType.CheckCLRTypes(res);
  135. }
  136. else
  137. return appdomain.Invoke(method, null, parameters);
  138. }
  139. public override bool IsDefined(Type attributeType, bool inherit)
  140. {
  141. if (customAttributes == null)
  142. InitializeCustomAttribute();
  143. for (int i = 0; i < customAttributes.Length; i++)
  144. {
  145. if (attributeTypes[i] == attributeType)
  146. return true;
  147. }
  148. return false;
  149. }
  150. public override Type ReturnType
  151. {
  152. get
  153. {
  154. return method.ReturnType?.ReflectionType;
  155. }
  156. }
  157. public override Delegate CreateDelegate(Type delegateType)
  158. {
  159. throw new NotSupportedException("please use CreateDelegate(Type delegateType, object target)");
  160. }
  161. private IDelegateAdapter iDelegate;
  162. public override Delegate CreateDelegate(Type delegateType, object target)
  163. {
  164. ILTypeInstance ilTypeInstance;
  165. if (target is ILTypeInstance)
  166. {
  167. ilTypeInstance = target as ILTypeInstance;
  168. }
  169. else if (target is CrossBindingAdaptorType adaptor)
  170. {
  171. ilTypeInstance = adaptor.ILInstance;
  172. }
  173. else
  174. {
  175. return null;
  176. }
  177. IDelegateAdapter del;
  178. if (iDelegate == null)
  179. {
  180. iDelegate = appdomain.DelegateManager.FindDelegateAdapter(ilTypeInstance, method, method);
  181. del = iDelegate;
  182. }
  183. else
  184. {
  185. del = iDelegate.Instantiate(appdomain, ilTypeInstance, iDelegate.Method);
  186. }
  187. return del.GetConvertor(delegateType);
  188. }
  189. }
  190. }