FieldInfo.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. //
  2. // System.Reflection.FieldInfo.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // (C) Ximian, Inc. http://www.ximian.com
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. using System.Diagnostics;
  30. using System.Globalization;
  31. using System.Runtime.CompilerServices;
  32. using System.Runtime.InteropServices;
  33. namespace System.Reflection {
  34. [ComVisible (true)]
  35. [ComDefaultInterfaceAttribute (typeof (_FieldInfo))]
  36. [Serializable]
  37. [ClassInterface(ClassInterfaceType.None)]
  38. #if MOBILE
  39. public abstract class FieldInfo : MemberInfo {
  40. #else
  41. public abstract class FieldInfo : MemberInfo, _FieldInfo {
  42. #endif
  43. public abstract FieldAttributes Attributes {get;}
  44. public abstract RuntimeFieldHandle FieldHandle {get;}
  45. protected FieldInfo () {}
  46. public abstract Type FieldType { get; }
  47. public abstract object GetValue(object obj);
  48. public override MemberTypes MemberType {
  49. get { return MemberTypes.Field;}
  50. }
  51. public bool IsLiteral
  52. {
  53. get {return (Attributes & FieldAttributes.Literal) != 0;}
  54. }
  55. public bool IsStatic
  56. {
  57. get {return (Attributes & FieldAttributes.Static) != 0;}
  58. }
  59. public bool IsInitOnly
  60. {
  61. get {return (Attributes & FieldAttributes.InitOnly) != 0;}
  62. }
  63. public Boolean IsPublic
  64. {
  65. get
  66. {
  67. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
  68. }
  69. }
  70. public Boolean IsPrivate
  71. {
  72. get
  73. {
  74. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
  75. }
  76. }
  77. public Boolean IsFamily
  78. {
  79. get
  80. {
  81. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
  82. }
  83. }
  84. public Boolean IsAssembly
  85. {
  86. get
  87. {
  88. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
  89. }
  90. }
  91. public Boolean IsFamilyAndAssembly
  92. {
  93. get {
  94. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
  95. }
  96. }
  97. public Boolean IsFamilyOrAssembly
  98. {
  99. get
  100. {
  101. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
  102. }
  103. }
  104. public Boolean IsPinvokeImpl
  105. {
  106. get
  107. {
  108. return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
  109. }
  110. }
  111. public Boolean IsSpecialName
  112. {
  113. get
  114. {
  115. return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
  116. }
  117. }
  118. public Boolean IsNotSerialized
  119. {
  120. get
  121. {
  122. return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
  123. }
  124. }
  125. public abstract void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
  126. [DebuggerHidden]
  127. [DebuggerStepThrough]
  128. public void SetValue (object obj, object value)
  129. {
  130. SetValue (obj, value, 0, null, null);
  131. }
  132. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  133. private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
  134. public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
  135. {
  136. if (handle.Value == IntPtr.Zero)
  137. throw new ArgumentException ("The handle is invalid.");
  138. return internal_from_handle_type (handle.Value, IntPtr.Zero);
  139. }
  140. [ComVisible (false)]
  141. public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
  142. {
  143. if (handle.Value == IntPtr.Zero)
  144. throw new ArgumentException ("The handle is invalid.");
  145. FieldInfo fi = internal_from_handle_type (handle.Value, declaringType.Value);
  146. if (fi == null)
  147. throw new ArgumentException ("The field handle and the type handle are incompatible.");
  148. return fi;
  149. }
  150. //
  151. // Note: making this abstract imposes an implementation requirement
  152. // on any class that derives from it. However, since it's also
  153. // internal, that means only classes inside corlib can derive
  154. // from FieldInfo. See
  155. //
  156. // errors/cs0534-4.cs errors/CS0534-4-lib.cs
  157. //
  158. // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
  159. //
  160. internal virtual int GetFieldOffset ()
  161. {
  162. throw new SystemException ("This method should not be called");
  163. }
  164. [CLSCompliant(false)]
  165. public virtual object GetValueDirect (TypedReference obj)
  166. {
  167. throw new NotSupportedException(Environment.GetResourceString("NotSupported_AbstractNonCLS"));
  168. }
  169. [CLSCompliant(false)]
  170. public virtual void SetValueDirect (TypedReference obj, object value)
  171. {
  172. throw new NotSupportedException(Environment.GetResourceString("NotSupported_AbstractNonCLS"));
  173. }
  174. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  175. private extern MarshalAsAttribute get_marshal_info ();
  176. internal object[] GetPseudoCustomAttributes ()
  177. {
  178. int count = 0;
  179. if (IsNotSerialized)
  180. count ++;
  181. if (DeclaringType.IsExplicitLayout)
  182. count ++;
  183. MarshalAsAttribute marshalAs = get_marshal_info ();
  184. if (marshalAs != null)
  185. count ++;
  186. if (count == 0)
  187. return null;
  188. object[] attrs = new object [count];
  189. count = 0;
  190. if (IsNotSerialized)
  191. attrs [count ++] = new NonSerializedAttribute ();
  192. if (DeclaringType.IsExplicitLayout)
  193. attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
  194. if (marshalAs != null)
  195. attrs [count ++] = marshalAs;
  196. return attrs;
  197. }
  198. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  199. extern Type[] GetTypeModifiers (bool optional);
  200. public virtual Type[] GetOptionalCustomModifiers () {
  201. Type[] types = GetTypeModifiers (true);
  202. if (types == null)
  203. return Type.EmptyTypes;
  204. return types;
  205. }
  206. public virtual Type[] GetRequiredCustomModifiers () {
  207. Type[] types = GetTypeModifiers (false);
  208. if (types == null)
  209. return Type.EmptyTypes;
  210. return types;
  211. }
  212. public virtual object GetRawConstantValue ()
  213. {
  214. throw new NotSupportedException ("This non-CLS method is not implemented.");
  215. }
  216. public override bool Equals (object obj)
  217. {
  218. return obj == (object) this;
  219. }
  220. public override int GetHashCode ()
  221. {
  222. return base.GetHashCode ();
  223. }
  224. public static bool operator == (FieldInfo left, FieldInfo right)
  225. {
  226. if ((object)left == (object)right)
  227. return true;
  228. if ((object)left == null ^ (object)right == null)
  229. return false;
  230. return left.Equals (right);
  231. }
  232. public static bool operator != (FieldInfo left, FieldInfo right)
  233. {
  234. if ((object)left == (object)right)
  235. return false;
  236. if ((object)left == null ^ (object)right == null)
  237. return true;
  238. return !left.Equals (right);
  239. }
  240. public virtual bool IsSecurityCritical {
  241. get {
  242. throw new NotSupportedException ();
  243. }
  244. }
  245. public virtual bool IsSecuritySafeCritical {
  246. get {
  247. throw new NotSupportedException ();
  248. }
  249. }
  250. public virtual bool IsSecurityTransparent {
  251. get {
  252. throw new NotSupportedException ();
  253. }
  254. }
  255. #if !MOBILE
  256. void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  257. {
  258. throw new NotImplementedException ();
  259. }
  260. Type _FieldInfo.GetType ()
  261. {
  262. // Required or object::GetType becomes virtual final
  263. return base.GetType ();
  264. }
  265. void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  266. {
  267. throw new NotImplementedException ();
  268. }
  269. void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
  270. {
  271. throw new NotImplementedException ();
  272. }
  273. void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  274. {
  275. throw new NotImplementedException ();
  276. }
  277. #endif
  278. }
  279. }