FieldInfo.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. //
  9. // TODO: Mucho left to implement.
  10. //
  11. //
  12. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.Diagnostics;
  35. using System.Reflection;
  36. using System.Reflection.Emit;
  37. using System.Globalization;
  38. using System.Runtime.CompilerServices;
  39. using System.Runtime.InteropServices;
  40. namespace System.Reflection {
  41. #if NET_2_0
  42. [ComVisible (true)]
  43. #endif
  44. [Serializable]
  45. [ClassInterface(ClassInterfaceType.None)]
  46. public abstract class FieldInfo : MemberInfo {
  47. public abstract FieldAttributes Attributes {get;}
  48. public abstract RuntimeFieldHandle FieldHandle {get;}
  49. protected FieldInfo () {}
  50. public abstract Type FieldType { get; }
  51. public abstract object GetValue(object obj);
  52. public override MemberTypes MemberType {
  53. get { return MemberTypes.Field;}
  54. }
  55. public bool IsLiteral
  56. {
  57. get {return (Attributes & FieldAttributes.Literal) != 0;}
  58. }
  59. public bool IsStatic
  60. {
  61. get {return (Attributes & FieldAttributes.Static) != 0;}
  62. }
  63. public bool IsInitOnly
  64. {
  65. get {return (Attributes & FieldAttributes.InitOnly) != 0;}
  66. }
  67. public Boolean IsPublic
  68. {
  69. get
  70. {
  71. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
  72. }
  73. }
  74. public Boolean IsPrivate
  75. {
  76. get
  77. {
  78. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
  79. }
  80. }
  81. public Boolean IsFamily
  82. {
  83. get
  84. {
  85. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
  86. }
  87. }
  88. public Boolean IsAssembly
  89. {
  90. get
  91. {
  92. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
  93. }
  94. }
  95. public Boolean IsFamilyAndAssembly
  96. {
  97. get {
  98. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
  99. }
  100. }
  101. public Boolean IsFamilyOrAssembly
  102. {
  103. get
  104. {
  105. return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
  106. }
  107. }
  108. public Boolean IsPinvokeImpl
  109. {
  110. get
  111. {
  112. return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
  113. }
  114. }
  115. public Boolean IsSpecialName
  116. {
  117. get
  118. {
  119. return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
  120. }
  121. }
  122. public Boolean IsNotSerialized
  123. {
  124. get
  125. {
  126. return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
  127. }
  128. }
  129. public abstract void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
  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 (IntPtr handle);
  138. public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
  139. {
  140. return internal_from_handle (handle.Value);
  141. }
  142. //
  143. // Note: making this abstract imposes an implementation requirement
  144. // on any class that derives from it. However, since it's also
  145. // internal, that means only classes inside corlib can derive
  146. // from FieldInfo. See
  147. //
  148. // errors/cs0534-4.cs errors/CS0534-4-lib.cs
  149. //
  150. // class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
  151. //
  152. internal virtual int GetFieldOffset ()
  153. {
  154. throw new SystemException ("This method should not be called");
  155. }
  156. [CLSCompliant(false)]
  157. [MonoTODO]
  158. public virtual object GetValueDirect (TypedReference obj)
  159. {
  160. throw new NotImplementedException ();
  161. }
  162. [CLSCompliant(false)]
  163. [MonoTODO]
  164. public virtual void SetValueDirect (TypedReference obj, object value)
  165. {
  166. throw new NotImplementedException ();
  167. }
  168. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  169. private extern UnmanagedMarshal GetUnmanagedMarshal ();
  170. internal object[] GetPseudoCustomAttributes ()
  171. {
  172. int count = 0;
  173. if (IsNotSerialized)
  174. count ++;
  175. if (DeclaringType.IsExplicitLayout)
  176. count ++;
  177. UnmanagedMarshal marshalAs = GetUnmanagedMarshal ();
  178. if (marshalAs != null)
  179. count ++;
  180. if (count == 0)
  181. return null;
  182. object[] attrs = new object [count];
  183. count = 0;
  184. if (IsNotSerialized)
  185. attrs [count ++] = new NonSerializedAttribute ();
  186. if (DeclaringType.IsExplicitLayout)
  187. attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
  188. if (marshalAs != null)
  189. attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
  190. return attrs;
  191. }
  192. #if NET_2_0 || BOOTSTRAP_NET_2_0
  193. public virtual Type[] OptionalCustomModifiers {
  194. get {
  195. throw new NotImplementedException ();
  196. }
  197. }
  198. public virtual Type[] RequiredCustomModifiers {
  199. get {
  200. throw new NotImplementedException ();
  201. }
  202. }
  203. #endif
  204. #if NET_2_0 || BOOTSTRAP_NET_2_0
  205. public abstract FieldInfo Mono_GetGenericFieldDefinition ();
  206. #endif
  207. }
  208. }