ILRuntimePropertyInfo.cs 7.0 KB

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