ILRuntimeMethodInfo.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. namespace ILRuntime.Reflection
  10. {
  11. public class ILRuntimeMethodInfo : MethodInfo
  12. {
  13. ILMethod method;
  14. ILRuntimeParameterInfo[] parameters;
  15. Mono.Cecil.MethodDefinition definition;
  16. ILRuntime.Runtime.Enviorment.AppDomain appdomain;
  17. object[] customAttributes;
  18. Type[] attributeTypes;
  19. public ILRuntimeMethodInfo(ILMethod m)
  20. {
  21. method = m;
  22. definition = m.Definition;
  23. appdomain = m.DeclearingType.AppDomain;
  24. parameters = new ILRuntimeParameterInfo[m.ParameterCount];
  25. for (int i = 0; i < m.ParameterCount; i++)
  26. {
  27. parameters[i] = new ILRuntimeParameterInfo(m.Parameters[i]);
  28. }
  29. }
  30. void InitializeCustomAttribute()
  31. {
  32. customAttributes = new object[definition.CustomAttributes.Count];
  33. attributeTypes = new Type[customAttributes.Length];
  34. for (int i = 0; i < definition.CustomAttributes.Count; i++)
  35. {
  36. var attribute = definition.CustomAttributes[i];
  37. var at = appdomain.GetType(attribute.AttributeType, null, null);
  38. try
  39. {
  40. object ins = attribute.CreateInstance(at, appdomain);
  41. attributeTypes[i] = at.ReflectionType;
  42. customAttributes[i] = ins;
  43. }
  44. catch
  45. {
  46. attributeTypes[i] = typeof(Attribute);
  47. }
  48. }
  49. }
  50. internal ILMethod ILMethod { get { return method; } }
  51. public override MethodAttributes Attributes
  52. {
  53. get
  54. {
  55. return MethodAttributes.Public;
  56. }
  57. }
  58. public override Type DeclaringType
  59. {
  60. get
  61. {
  62. return method.DeclearingType.ReflectionType;
  63. }
  64. }
  65. public override RuntimeMethodHandle MethodHandle
  66. {
  67. get
  68. {
  69. throw new NotImplementedException();
  70. }
  71. }
  72. public override string Name
  73. {
  74. get
  75. {
  76. return method.Name;
  77. }
  78. }
  79. public override Type ReflectedType
  80. {
  81. get
  82. {
  83. return method.DeclearingType.ReflectionType;
  84. }
  85. }
  86. public override ICustomAttributeProvider ReturnTypeCustomAttributes
  87. {
  88. get
  89. {
  90. throw new NotImplementedException();
  91. }
  92. }
  93. public override MethodInfo GetBaseDefinition()
  94. {
  95. return this;
  96. }
  97. public override object[] GetCustomAttributes(bool inherit)
  98. {
  99. if (customAttributes == null)
  100. InitializeCustomAttribute();
  101. return customAttributes;
  102. }
  103. public override object[] GetCustomAttributes(Type attributeType, bool inherit)
  104. {
  105. if (customAttributes == null)
  106. InitializeCustomAttribute();
  107. List<object> res = new List<object>();
  108. for (int i = 0; i < customAttributes.Length; i++)
  109. {
  110. if (attributeTypes[i] == attributeType)
  111. res.Add(customAttributes[i]);
  112. }
  113. return res.ToArray();
  114. }
  115. public override MethodImplAttributes GetMethodImplementationFlags()
  116. {
  117. throw new NotImplementedException();
  118. }
  119. public override ParameterInfo[] GetParameters()
  120. {
  121. throw new NotImplementedException();
  122. }
  123. public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture)
  124. {
  125. if (method.HasThis)
  126. {
  127. var res = appdomain.Invoke(method, obj, parameters);
  128. return ReturnType.CheckCLRTypes(res);
  129. }
  130. else
  131. return appdomain.Invoke(method, null, parameters);
  132. }
  133. public override bool IsDefined(Type attributeType, bool inherit)
  134. {
  135. if (customAttributes == null)
  136. InitializeCustomAttribute();
  137. for (int i = 0; i < customAttributes.Length; i++)
  138. {
  139. if (attributeTypes[i] == attributeType)
  140. return true;
  141. }
  142. return false;
  143. }
  144. public override Type ReturnType
  145. {
  146. get
  147. {
  148. return method.ReturnType.ReflectionType;
  149. }
  150. }
  151. }
  152. }