ILRuntimeFieldInfo.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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.Mono.Cecil;
  8. using ILRuntime.CLR.Utils;
  9. using ILRuntime.CLR.TypeSystem;
  10. using ILRuntime.Runtime;
  11. using ILRuntime.Runtime.Stack;
  12. using ILRuntime.Runtime.Enviorment;
  13. using ILRuntime.Runtime.Intepreter;
  14. namespace ILRuntime.Reflection
  15. {
  16. public class ILRuntimeFieldInfo : FieldInfo
  17. {
  18. System.Reflection.FieldAttributes attr;
  19. ILRuntimeType dType;
  20. ILType ilType;
  21. IType fieldType;
  22. bool isStatic;
  23. int fieldIdx;
  24. string name;
  25. FieldDefinition definition;
  26. Runtime.Enviorment.AppDomain appdomain;
  27. Attribute[] customAttributes;
  28. Type[] attributeTypes;
  29. public IType ILFieldType { get { return fieldType; } }
  30. public ILRuntimeFieldInfo(FieldDefinition def, ILRuntimeType declaredType, bool isStatic, int fieldIdx)
  31. {
  32. definition = def;
  33. this.name = def.Name;
  34. dType = declaredType;
  35. ilType = dType.ILType;
  36. appdomain = ilType.AppDomain;
  37. this.isStatic = isStatic;
  38. this.fieldIdx = fieldIdx;
  39. if (isStatic)
  40. attr |= System.Reflection.FieldAttributes.Static;
  41. if (def.IsPublic)
  42. {
  43. attr |= System.Reflection.FieldAttributes.Public;
  44. }
  45. else
  46. attr |= System.Reflection.FieldAttributes.Private;
  47. fieldType = isStatic ? ilType.StaticFieldTypes[fieldIdx] : ilType.FieldTypes[fieldIdx];
  48. }
  49. public ILRuntimeFieldInfo(FieldDefinition def, ILRuntimeType declaredType, int fieldIdx, IType fieldType)
  50. {
  51. definition = def;
  52. this.name = def.Name;
  53. dType = declaredType;
  54. ilType = dType.ILType;
  55. appdomain = ilType.AppDomain;
  56. this.isStatic = false;
  57. this.fieldIdx = fieldIdx;
  58. if (isStatic)
  59. attr |= System.Reflection.FieldAttributes.Static;
  60. if (def.IsPublic)
  61. {
  62. attr |= System.Reflection.FieldAttributes.Public;
  63. }
  64. else
  65. attr |= System.Reflection.FieldAttributes.Private;
  66. this.fieldType = fieldType;
  67. }
  68. void InitializeCustomAttribute()
  69. {
  70. customAttributes = new Attribute[definition.CustomAttributes.Count];
  71. attributeTypes = new Type[customAttributes.Length];
  72. for (int i = 0; i < definition.CustomAttributes.Count; i++)
  73. {
  74. var attribute = definition.CustomAttributes[i];
  75. var at = appdomain.GetType(attribute.AttributeType, null, null);
  76. try
  77. {
  78. Attribute ins = attribute.CreateInstance(at, appdomain) as Attribute;
  79. attributeTypes[i] = at.ReflectionType;
  80. customAttributes[i] = ins;
  81. }
  82. catch
  83. {
  84. attributeTypes[i] = typeof(Attribute);
  85. }
  86. }
  87. }
  88. public override System.Reflection.FieldAttributes Attributes
  89. {
  90. get
  91. {
  92. return attr;
  93. }
  94. }
  95. public override Type DeclaringType
  96. {
  97. get
  98. {
  99. return dType;
  100. }
  101. }
  102. public override RuntimeFieldHandle FieldHandle
  103. {
  104. get
  105. {
  106. throw new NotImplementedException();
  107. }
  108. }
  109. public override Type FieldType
  110. {
  111. get
  112. {
  113. return fieldType.ReflectionType;
  114. }
  115. }
  116. public override string Name
  117. {
  118. get
  119. {
  120. return name;
  121. }
  122. }
  123. public override Type ReflectedType
  124. {
  125. get
  126. {
  127. return fieldType.ReflectionType;
  128. }
  129. }
  130. public override object[] GetCustomAttributes(bool inherit)
  131. {
  132. if (customAttributes == null)
  133. InitializeCustomAttribute();
  134. return customAttributes;
  135. }
  136. public override object[] GetCustomAttributes(Type attributeType, bool inherit)
  137. {
  138. if (customAttributes == null)
  139. InitializeCustomAttribute();
  140. List<object> res = new List<object>();
  141. for (int i = 0; i < customAttributes.Length; i++)
  142. {
  143. if (attributeTypes[i].Equals(attributeType) || attributeTypes[i].IsSubclassOf(attributeType))
  144. {
  145. res.Add(customAttributes[i]);
  146. }
  147. }
  148. return res.ToArray();
  149. }
  150. public override object GetValue(object obj)
  151. {
  152. unsafe
  153. {
  154. ILTypeInstance ins;
  155. if (isStatic)
  156. {
  157. ins = ilType.StaticInstance;
  158. }
  159. else
  160. {
  161. if (obj is ILTypeInstance)
  162. ins = (ILTypeInstance)obj;
  163. else
  164. ins = ((CrossBindingAdaptorType)obj).ILInstance;
  165. }
  166. return fieldType.TypeForCLR.CheckCLRTypes(ins[fieldIdx]);
  167. }
  168. }
  169. public override bool IsDefined(Type attributeType, bool inherit)
  170. {
  171. if (customAttributes == null)
  172. InitializeCustomAttribute();
  173. for (int i = 0; i < customAttributes.Length; i++)
  174. {
  175. if (attributeTypes[i].Equals(attributeType))
  176. {
  177. return true;
  178. }
  179. }
  180. return false;
  181. }
  182. public override void SetValue(object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
  183. {
  184. unsafe
  185. {
  186. if (value is CrossBindingAdaptorType)
  187. value = ((CrossBindingAdaptorType)value).ILInstance;
  188. ILTypeInstance ins;
  189. if (isStatic)
  190. {
  191. ins = ilType.StaticInstance;
  192. }
  193. else
  194. {
  195. if (obj is ILTypeInstance)
  196. ins = (ILTypeInstance)obj;
  197. else
  198. ins = ((CrossBindingAdaptorType)obj).ILInstance;
  199. }
  200. ins[fieldIdx] = value;
  201. }
  202. }
  203. }
  204. }