ILRuntimePropertyInfo.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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
  129. {
  130. get
  131. {
  132. return definition.GetMethod != null ? definition.GetMethod.ReturnType : definition.SetMethod.Parameters[0].ParameterType;
  133. }
  134. }
  135. public override Type DeclaringType
  136. {
  137. get
  138. {
  139. return dType.ReflectionType;
  140. }
  141. }
  142. public override object[] GetCustomAttributes(bool inherit)
  143. {
  144. if (customAttributes == null)
  145. InitializeCustomAttribute();
  146. return customAttributes;
  147. }
  148. public override object[] GetCustomAttributes(Type attributeType, bool inherit)
  149. {
  150. if (customAttributes == null)
  151. InitializeCustomAttribute();
  152. List<object> res = new List<object>();
  153. for (int i = 0; i < customAttributes.Length; i++)
  154. {
  155. if (attributeTypes[i].Equals(attributeType))
  156. {
  157. res.Add(customAttributes[i]);
  158. }
  159. }
  160. return res.ToArray();
  161. }
  162. public override bool IsDefined(Type attributeType, bool inherit)
  163. {
  164. if (customAttributes == null)
  165. InitializeCustomAttribute();
  166. for (int i = 0; i < customAttributes.Length; i++)
  167. {
  168. if (attributeTypes[i].Equals(attributeType))
  169. {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. public override MethodInfo[] GetAccessors(bool nonPublic)
  176. {
  177. throw new NotImplementedException();
  178. }
  179. public override MethodInfo GetGetMethod(bool nonPublic)
  180. {
  181. if (getter != null)
  182. return getter.ReflectionMethodInfo;
  183. return null;
  184. }
  185. public override ParameterInfo[] GetIndexParameters()
  186. {
  187. return new ParameterInfo[0];
  188. }
  189. public override MethodInfo GetSetMethod(bool nonPublic)
  190. {
  191. if (setter != null)
  192. return setter.ReflectionMethodInfo;
  193. return null;
  194. }
  195. public override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  196. {
  197. var indexCnt = index != null ? index.Length : 0;
  198. if (getter.ParameterCount <= indexCnt)
  199. {
  200. using (var ctx = appdomain.BeginInvoke(getter))
  201. {
  202. if (!IsStatic)
  203. ctx.PushObject(obj);
  204. for (int i = 0; i < getter.ParameterCount; i++)
  205. {
  206. ctx.PushObject(index[i], !getter.Parameters[i].IsValueType);
  207. }
  208. ctx.Invoke();
  209. return ctx.ReadObject(getter.ReturnType.TypeForCLR);
  210. }
  211. }
  212. else
  213. throw new ArgumentException("Index count mismatch");
  214. }
  215. public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  216. {
  217. var indexCnt = index != null ? index.Length : 0;
  218. if (setter.ParameterCount <= indexCnt + 1)
  219. {
  220. using (var ctx = appdomain.BeginInvoke(setter))
  221. {
  222. if (!IsStatic)
  223. ctx.PushObject(obj);
  224. for (int i = 0; i < setter.ParameterCount - 1; i++)
  225. {
  226. ctx.PushObject(index[i], !setter.Parameters[i].IsValueType);
  227. }
  228. ctx.PushObject(value, !setter.Parameters[setter.ParameterCount - 1].IsValueType);
  229. ctx.Invoke();
  230. }
  231. }
  232. else
  233. throw new ArgumentException("Index count mismatch");
  234. }
  235. }
  236. }