ILRuntimePropertyInfo.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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.TypeSystem;
  9. namespace ILRuntime.Reflection
  10. {
  11. public class ILRuntimePropertyInfo : PropertyInfo
  12. {
  13. ILMethod getter, setter;
  14. ILType dType;
  15. Mono.Cecil.PropertyDefinition definition;
  16. ILRuntime.Runtime.Enviorment.AppDomain appdomain;
  17. Attribute[] customAttributes;
  18. Type[] attributeTypes;
  19. public ILMethod Getter
  20. {
  21. get { return getter; }
  22. set
  23. {
  24. getter = value;
  25. }
  26. }
  27. public ILMethod Setter
  28. {
  29. get { return setter; }
  30. set
  31. {
  32. setter = value;
  33. }
  34. }
  35. public bool IsPublic
  36. {
  37. get
  38. {
  39. if (getter != null)
  40. return getter.Definition.IsPublic;
  41. else
  42. return setter.Definition.IsPublic;
  43. }
  44. }
  45. public bool IsStatic
  46. {
  47. get
  48. {
  49. if (getter != null)
  50. return getter.IsStatic;
  51. else
  52. return setter.IsStatic;
  53. }
  54. }
  55. public ILRuntimePropertyInfo(Mono.Cecil.PropertyDefinition definition, ILType dType)
  56. {
  57. this.definition = definition;
  58. this.dType = dType;
  59. appdomain = dType.AppDomain;
  60. }
  61. void InitializeCustomAttribute()
  62. {
  63. customAttributes = new Attribute[definition.CustomAttributes.Count];
  64. attributeTypes = new Type[customAttributes.Length];
  65. for (int i = 0; i < definition.CustomAttributes.Count; i++)
  66. {
  67. var attribute = definition.CustomAttributes[i];
  68. var at = appdomain.GetType(attribute.AttributeType, null, null);
  69. try
  70. {
  71. Attribute ins = attribute.CreateInstance(at, appdomain) as Attribute;
  72. attributeTypes[i] = at.ReflectionType;
  73. customAttributes[i] = ins;
  74. }
  75. catch
  76. {
  77. attributeTypes[i] = typeof(Attribute);
  78. }
  79. }
  80. }
  81. public override string Name
  82. {
  83. get
  84. {
  85. return definition.Name;
  86. }
  87. }
  88. public override Type ReflectedType
  89. {
  90. get
  91. {
  92. return dType.ReflectionType;
  93. }
  94. }
  95. public override PropertyAttributes Attributes
  96. {
  97. get
  98. {
  99. return PropertyAttributes.None;
  100. }
  101. }
  102. public override bool CanRead
  103. {
  104. get
  105. {
  106. return getter != null;
  107. }
  108. }
  109. public override bool CanWrite
  110. {
  111. get
  112. {
  113. return setter != null;
  114. }
  115. }
  116. public override Type PropertyType
  117. {
  118. get
  119. {
  120. if (getter != null)
  121. return getter.ReturnType.ReflectionType;
  122. else
  123. {
  124. return setter.Parameters[0].ReflectionType;
  125. }
  126. }
  127. }
  128. public ILRuntime.Mono.Cecil.TypeReference Definition => definition.GetMethod != null ? definition.GetMethod.ReturnType : definition.SetMethod.Parameters [ 0 ].ParameterType;
  129. public override Type DeclaringType
  130. {
  131. get
  132. {
  133. return dType.ReflectionType;
  134. }
  135. }
  136. public override object[] GetCustomAttributes(bool inherit)
  137. {
  138. if (customAttributes == null)
  139. InitializeCustomAttribute();
  140. return customAttributes;
  141. }
  142. public override object[] GetCustomAttributes(Type attributeType, bool inherit)
  143. {
  144. if (customAttributes == null)
  145. InitializeCustomAttribute();
  146. List<object> res = new List<object>();
  147. for (int i = 0; i < customAttributes.Length; i++)
  148. {
  149. if (attributeTypes[i].Equals(attributeType))
  150. {
  151. res.Add(customAttributes[i]);
  152. }
  153. }
  154. return res.ToArray();
  155. }
  156. public override bool IsDefined(Type attributeType, bool inherit)
  157. {
  158. if (customAttributes == null)
  159. InitializeCustomAttribute();
  160. for (int i = 0; i < customAttributes.Length; i++)
  161. {
  162. if (attributeTypes[i].Equals(attributeType))
  163. {
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. public override MethodInfo[] GetAccessors(bool nonPublic)
  170. {
  171. throw new NotImplementedException();
  172. }
  173. public override MethodInfo GetGetMethod(bool nonPublic)
  174. {
  175. if (getter != null)
  176. return getter.ReflectionMethodInfo;
  177. return null;
  178. }
  179. public override ParameterInfo[] GetIndexParameters()
  180. {
  181. return new ParameterInfo[0];
  182. }
  183. public override MethodInfo GetSetMethod(bool nonPublic)
  184. {
  185. if (setter != null)
  186. return setter.ReflectionMethodInfo;
  187. return null;
  188. }
  189. public override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  190. {
  191. var indexCnt = index != null ? index.Length : 0;
  192. if (getter.ParameterCount <= indexCnt)
  193. {
  194. using (var ctx = appdomain.BeginInvoke(getter))
  195. {
  196. if (!IsStatic)
  197. ctx.PushObject(obj);
  198. for (int i = 0; i < getter.ParameterCount; i++)
  199. {
  200. ctx.PushObject(index[i], !getter.Parameters[i].IsValueType);
  201. }
  202. ctx.Invoke();
  203. return ctx.ReadObject(getter.ReturnType.TypeForCLR);
  204. }
  205. }
  206. else
  207. throw new ArgumentException("Index count mismatch");
  208. }
  209. public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  210. {
  211. var indexCnt = index != null ? index.Length : 0;
  212. if (setter.ParameterCount <= indexCnt + 1)
  213. {
  214. using (var ctx = appdomain.BeginInvoke(setter))
  215. {
  216. if (!IsStatic)
  217. ctx.PushObject(obj);
  218. for (int i = 0; i < setter.ParameterCount - 1; i++)
  219. {
  220. ctx.PushObject(index[i], !setter.Parameters[i].IsValueType);
  221. }
  222. ctx.PushObject(value, !setter.Parameters[setter.ParameterCount - 1].IsValueType);
  223. ctx.Invoke();
  224. }
  225. }
  226. else
  227. throw new ArgumentException("Index count mismatch");
  228. }
  229. }
  230. }