FieldInfo.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. #if NET_2_0
  35. [ComVisible (true)]
  36. [ComDefaultInterfaceAttribute (typeof (_FieldInfo))]
  37. #endif
  38. [Serializable]
  39. [ClassInterface(ClassInterfaceType.None)]
  40. public abstract class FieldInfo : MemberInfo, _FieldInfo {
  41. public abstract FieldAttributes Attributes {get;}
  42. public abstract RuntimeFieldHandle FieldHandle {get;}
  43. protected FieldInfo () {}
  44. public abstract Type FieldType { get; }
  45. public abstract object GetValue(object obj);
  46. public override MemberTypes MemberType {
  47. get { return MemberTypes.Field;}
  48. }
  49. public bool IsLiteral
  50. {
  51. get {return (Attributes & FieldAttributes.Literal) != 0;}
  52. }
  53. public bool IsStatic
  54. {
  55. get {return (Attributes & FieldAttributes.Static) != 0;}
  56. }
  57. public bool IsInitOnly
  58. {
  59. get {return (Attributes & FieldAttributes.InitOnly) != 0;}
  60. }
  61. public Boolean IsPublic
  62. {
  63. get
  64. {
  65. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
  66. }
  67. }
  68. public Boolean IsPrivate
  69. {
  70. get
  71. {
  72. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
  73. }
  74. }
  75. public Boolean IsFamily
  76. {
  77. get
  78. {
  79. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
  80. }
  81. }
  82. public Boolean IsAssembly
  83. {
  84. get
  85. {
  86. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
  87. }
  88. }
  89. public Boolean IsFamilyAndAssembly
  90. {
  91. get {
  92. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
  93. }
  94. }
  95. public Boolean IsFamilyOrAssembly
  96. {
  97. get
  98. {
  99. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
  100. }
  101. }
  102. public Boolean IsPinvokeImpl
  103. {
  104. get
  105. {
  106. return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
  107. }
  108. }
  109. public Boolean IsSpecialName
  110. {
  111. get
  112. {
  113. return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
  114. }
  115. }
  116. public Boolean IsNotSerialized
  117. {
  118. get
  119. {
  120. return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
  121. }
  122. }
  123. public abstract void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
  124. #if ONLY_1_1
  125. public new Type GetType ()
  126. {
  127. return base.GetType ();
  128. }
  129. #endif
  130. [DebuggerHidden]
  131. [DebuggerStepThrough]
  132. public void SetValue (object obj, object value)
  133. {
  134. SetValue (obj, value, 0, null, null);
  135. }
  136. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  137. private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
  138. public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
  139. {
  140. if (handle.Value == IntPtr.Zero)
  141. throw new ArgumentException ("The handle is invalid.");
  142. return internal_from_handle_type (handle.Value, IntPtr.Zero);
  143. }
  144. #if NET_2_0
  145. [ComVisible (false)]
  146. public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
  147. {
  148. if (handle.Value == IntPtr.Zero)
  149. throw new ArgumentException ("The handle is invalid.");
  150. return internal_from_handle_type (handle.Value, declaringType.Value);
  151. }
  152. #endif
  153. //
  154. // Note: making this abstract imposes an implementation requirement
  155. // on any class that derives from it. However, since it's also
  156. // internal, that means only classes inside corlib can derive
  157. // from FieldInfo. See
  158. //
  159. // errors/cs0534-4.cs errors/CS0534-4-lib.cs
  160. //
  161. // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
  162. //
  163. internal virtual int GetFieldOffset ()
  164. {
  165. throw new SystemException ("This method should not be called");
  166. }
  167. [CLSCompliant(false)]
  168. [MonoTODO("Not implemented")]
  169. public virtual object GetValueDirect (TypedReference obj)
  170. {
  171. throw new NotImplementedException ();
  172. }
  173. [CLSCompliant(false)]
  174. [MonoTODO("Not implemented")]
  175. public virtual void SetValueDirect (TypedReference obj, object value)
  176. {
  177. throw new NotImplementedException ();
  178. }
  179. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  180. private extern UnmanagedMarshal GetUnmanagedMarshal ();
  181. internal virtual UnmanagedMarshal UMarshal {
  182. get {
  183. return GetUnmanagedMarshal ();
  184. }
  185. }
  186. internal object[] GetPseudoCustomAttributes ()
  187. {
  188. int count = 0;
  189. if (IsNotSerialized)
  190. count ++;
  191. if (DeclaringType.IsExplicitLayout)
  192. count ++;
  193. UnmanagedMarshal marshalAs = UMarshal;
  194. if (marshalAs != null)
  195. count ++;
  196. if (count == 0)
  197. return null;
  198. object[] attrs = new object [count];
  199. count = 0;
  200. if (IsNotSerialized)
  201. attrs [count ++] = new NonSerializedAttribute ();
  202. if (DeclaringType.IsExplicitLayout)
  203. attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
  204. if (marshalAs != null)
  205. attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
  206. return attrs;
  207. }
  208. #if NET_2_0 || BOOTSTRAP_NET_2_0
  209. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  210. extern Type[] GetTypeModifiers (bool optional);
  211. public virtual Type[] GetOptionalCustomModifiers () {
  212. Type[] types = GetTypeModifiers (true);
  213. if (types == null)
  214. return Type.EmptyTypes;
  215. return types;
  216. }
  217. public virtual Type[] GetRequiredCustomModifiers () {
  218. Type[] types = GetTypeModifiers (false);
  219. if (types == null)
  220. return Type.EmptyTypes;
  221. return types;
  222. }
  223. public virtual object GetRawConstantValue ()
  224. {
  225. throw new NotSupportedException ("This non-CLS method is not implemented.");
  226. }
  227. #endif
  228. void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
  229. {
  230. throw new NotImplementedException ();
  231. }
  232. void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
  233. {
  234. throw new NotImplementedException ();
  235. }
  236. void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
  237. {
  238. throw new NotImplementedException ();
  239. }
  240. void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
  241. {
  242. throw new NotImplementedException ();
  243. }
  244. }
  245. }