FieldInfo.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. [DebuggerHidden]
  128. [DebuggerStepThrough]
  129. public void SetValue (object obj, object value)
  130. {
  131. SetValue (obj, value, 0, null, null);
  132. }
  133. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  134. private static extern FieldInfo internal_from_handle (IntPtr handle);
  135. public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
  136. {
  137. return internal_from_handle (handle.Value);
  138. }
  139. //
  140. // Note: making this abstract imposes an implementation requirement
  141. // on any class that derives from it. However, since it's also
  142. // internal, that means only classes inside corlib can derive
  143. // from FieldInfo. See
  144. //
  145. // errors/cs0534-4.cs errors/CS0534-4-lib.cs
  146. //
  147. // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
  148. //
  149. internal virtual int GetFieldOffset ()
  150. {
  151. throw new SystemException ("This method should not be called");
  152. }
  153. [CLSCompliant(false)]
  154. [MonoTODO]
  155. public virtual object GetValueDirect (TypedReference obj)
  156. {
  157. throw new NotImplementedException ();
  158. }
  159. [CLSCompliant(false)]
  160. [MonoTODO]
  161. public virtual void SetValueDirect (TypedReference obj, object value)
  162. {
  163. throw new NotImplementedException ();
  164. }
  165. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  166. private extern UnmanagedMarshal GetUnmanagedMarshal ();
  167. internal object[] GetPseudoCustomAttributes ()
  168. {
  169. int count = 0;
  170. if (IsNotSerialized)
  171. count ++;
  172. if (DeclaringType.IsExplicitLayout)
  173. count ++;
  174. UnmanagedMarshal marshalAs = GetUnmanagedMarshal ();
  175. if (marshalAs != null)
  176. count ++;
  177. if (count == 0)
  178. return null;
  179. object[] attrs = new object [count];
  180. count = 0;
  181. if (IsNotSerialized)
  182. attrs [count ++] = new NonSerializedAttribute ();
  183. if (DeclaringType.IsExplicitLayout)
  184. attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
  185. if (marshalAs != null)
  186. attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
  187. return attrs;
  188. }
  189. #if NET_2_0 || BOOTSTRAP_NET_2_0
  190. [Obsolete ("Use FieldInfo.GetOptionalCustomModifiers().")]
  191. public virtual Type[] OptionalCustomModifiers {
  192. get {
  193. return GetOptionalCustomModifiers ();
  194. }
  195. }
  196. [Obsolete ("Use FieldInfo.GetRequiredCustomModifiers().")]
  197. public virtual Type[] RequiredCustomModifiers {
  198. get {
  199. return GetRequiredCustomModifiers ();
  200. }
  201. }
  202. [MonoTODO]
  203. public virtual Type[] GetOptionalCustomModifiers () {
  204. throw new NotImplementedException ();
  205. }
  206. [MonoTODO]
  207. public virtual Type[] GetRequiredCustomModifiers () {
  208. throw new NotImplementedException ();
  209. }
  210. #endif
  211. #if NET_2_0 || BOOTSTRAP_NET_2_0
  212. public abstract FieldInfo Mono_GetGenericFieldDefinition ();
  213. #endif
  214. }
  215. }