FieldInfo.cs 8.0 KB

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