FieldInfo.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. [Serializable]
  42. [ClassInterface(ClassInterfaceType.AutoDual)]
  43. public abstract class FieldInfo : MemberInfo {
  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. internal abstract int GetFieldOffset ();
  140. [CLSCompliant(false)]
  141. [MonoTODO]
  142. public virtual object GetValueDirect (TypedReference obj)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. [CLSCompliant(false)]
  147. [MonoTODO]
  148. public virtual void SetValueDirect (TypedReference obj, object value)
  149. {
  150. throw new NotImplementedException ();
  151. }
  152. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  153. private extern UnmanagedMarshal GetUnmanagedMarshal ();
  154. internal object[] GetPseudoCustomAttributes ()
  155. {
  156. int count = 0;
  157. if (IsNotSerialized)
  158. count ++;
  159. if (DeclaringType.IsExplicitLayout)
  160. count ++;
  161. UnmanagedMarshal marshalAs = GetUnmanagedMarshal ();
  162. if (marshalAs != null)
  163. count ++;
  164. if (count == 0)
  165. return null;
  166. object[] attrs = new object [count];
  167. count = 0;
  168. if (IsNotSerialized)
  169. attrs [count ++] = new NonSerializedAttribute ();
  170. if (DeclaringType.IsExplicitLayout)
  171. attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
  172. if (marshalAs != null)
  173. attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
  174. return attrs;
  175. }
  176. #if NET_2_0 || BOOTSTRAP_NET_2_0
  177. public virtual Type[] OptionalCustomModifiers {
  178. get {
  179. throw new NotImplementedException ();
  180. }
  181. }
  182. public virtual Type[] RequiredCustomModifiers {
  183. get {
  184. throw new NotImplementedException ();
  185. }
  186. }
  187. #endif
  188. #if NET_2_0 || BOOTSTRAP_NET_2_0
  189. public abstract FieldInfo Mono_GetGenericFieldDefinition ();
  190. #endif
  191. }
  192. }