ILRuntimePropertyInfo.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. object[] customAttributes;
  18. Type[] attributeTypes;
  19. static object[] param = new object[1];
  20. public ILMethod Getter
  21. {
  22. get { return getter; }
  23. set
  24. {
  25. getter = value;
  26. }
  27. }
  28. public ILMethod Setter
  29. {
  30. get { return setter; }
  31. set
  32. {
  33. setter = value;
  34. }
  35. }
  36. public bool IsPublic
  37. {
  38. get
  39. {
  40. if (getter != null)
  41. return getter.Definition.IsPublic;
  42. else
  43. return setter.Definition.IsPublic;
  44. }
  45. }
  46. public bool IsStatic
  47. {
  48. get
  49. {
  50. if (getter != null)
  51. return getter.IsStatic;
  52. else
  53. return setter.IsStatic;
  54. }
  55. }
  56. public ILRuntimePropertyInfo(Mono.Cecil.PropertyDefinition definition, ILType dType)
  57. {
  58. this.definition = definition;
  59. this.dType = dType;
  60. appdomain = dType.AppDomain;
  61. }
  62. void InitializeCustomAttribute()
  63. {
  64. customAttributes = new object[definition.CustomAttributes.Count];
  65. attributeTypes = new Type[customAttributes.Length];
  66. for (int i = 0; i < definition.CustomAttributes.Count; i++)
  67. {
  68. var attribute = definition.CustomAttributes[i];
  69. var at = appdomain.GetType(attribute.AttributeType, null, null);
  70. try
  71. {
  72. object ins = attribute.CreateInstance(at, appdomain);
  73. attributeTypes[i] = at.ReflectionType;
  74. customAttributes[i] = ins;
  75. }
  76. catch
  77. {
  78. attributeTypes[i] = typeof(Attribute);
  79. }
  80. }
  81. }
  82. public override string Name
  83. {
  84. get
  85. {
  86. return definition.Name;
  87. }
  88. }
  89. public override Type ReflectedType
  90. {
  91. get
  92. {
  93. return dType.ReflectionType;
  94. }
  95. }
  96. public override PropertyAttributes Attributes
  97. {
  98. get
  99. {
  100. return PropertyAttributes.None;
  101. }
  102. }
  103. public override bool CanRead
  104. {
  105. get
  106. {
  107. return getter != null;
  108. }
  109. }
  110. public override bool CanWrite
  111. {
  112. get
  113. {
  114. return setter != null;
  115. }
  116. }
  117. public override Type PropertyType
  118. {
  119. get
  120. {
  121. if (getter != null)
  122. return getter.ReturnType.ReflectionType;
  123. else
  124. {
  125. return setter.Parameters[0].ReflectionType;
  126. }
  127. }
  128. }
  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] == attributeType)
  150. res.Add(customAttributes[i]);
  151. }
  152. return res.ToArray();
  153. }
  154. public override bool IsDefined(Type attributeType, bool inherit)
  155. {
  156. if (customAttributes == null)
  157. InitializeCustomAttribute();
  158. for (int i = 0; i < customAttributes.Length; i++)
  159. {
  160. if (attributeTypes[i] == attributeType)
  161. return true;
  162. }
  163. return false;
  164. }
  165. public override MethodInfo[] GetAccessors(bool nonPublic)
  166. {
  167. throw new NotImplementedException();
  168. }
  169. public override MethodInfo GetGetMethod(bool nonPublic)
  170. {
  171. if (getter != null)
  172. return getter.ReflectionMethodInfo;
  173. return null;
  174. }
  175. public override ParameterInfo[] GetIndexParameters()
  176. {
  177. throw new NotImplementedException();
  178. }
  179. public override MethodInfo GetSetMethod(bool nonPublic)
  180. {
  181. if (setter != null)
  182. return setter.ReflectionMethodInfo;
  183. return null;
  184. }
  185. public override object GetValue(object obj, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  186. {
  187. return appdomain.Invoke(getter, obj, null);
  188. }
  189. public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, object[] index, CultureInfo culture)
  190. {
  191. param[0] = value;
  192. appdomain.Invoke(setter, obj, param);
  193. }
  194. }
  195. }