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