FieldInfo.cs 7.0 KB

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