Merge pull request #487 from mayerwin/patch-1
[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 // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 using System.Diagnostics;
31 using System.Globalization;
32 using System.Runtime.CompilerServices;
33 using System.Runtime.InteropServices;
34
35 namespace System.Reflection {
36
37         [ComVisible (true)]
38         [ComDefaultInterfaceAttribute (typeof (_FieldInfo))]
39         [Serializable]
40         [ClassInterface(ClassInterfaceType.None)]
41         public abstract class FieldInfo : MemberInfo, _FieldInfo {
42
43                 public abstract FieldAttributes Attributes {get;}
44                 public abstract RuntimeFieldHandle FieldHandle {get;}
45
46                 protected FieldInfo () {}
47                 
48                 public abstract Type FieldType { get; }
49
50                 public abstract object GetValue(object obj);
51
52                 public override MemberTypes MemberType {
53                         get { return MemberTypes.Field;}
54                 }
55
56                 public bool IsLiteral
57                 {
58                         get {return (Attributes & FieldAttributes.Literal) != 0;}
59                 } 
60
61                 public bool IsStatic
62                 {
63                         get {return (Attributes & FieldAttributes.Static) != 0;}
64                 } 
65
66                 public bool IsInitOnly
67                 {
68                         get {return (Attributes & FieldAttributes.InitOnly) != 0;}
69                 } 
70                 public Boolean IsPublic
71                 { 
72                         get
73                         {
74                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
75                         }
76                 }
77                 public Boolean IsPrivate
78                 {
79                         get
80                         {
81                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
82                         }
83                 }
84                 public Boolean IsFamily
85                 {
86                         get
87                         {
88                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
89                         }
90                 }
91                 public Boolean IsAssembly
92                 {
93                         get
94                         {
95                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
96                         }
97                 }
98                 public Boolean IsFamilyAndAssembly
99                 {
100                         get {
101                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
102                         }
103                 }
104                 public Boolean IsFamilyOrAssembly
105                 {
106                         get
107                         {
108                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
109                         }
110                 }
111                 public Boolean IsPinvokeImpl
112                 {
113                         get
114                         {
115                                 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
116                         }
117                 }
118                 public Boolean IsSpecialName
119                 {
120                         get
121                         {
122                                 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
123                         }
124                 }
125                 public Boolean IsNotSerialized
126                 {
127                         get
128                         {
129                                 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
130                         }
131                 }
132
133                 public abstract void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
134
135                 [DebuggerHidden]
136                 [DebuggerStepThrough]
137                 public void SetValue (object obj, object value)
138                 {
139                         SetValue (obj, value, 0, null, null);
140                 }
141
142                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
143                 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
144
145                 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
146                 {
147                         if (handle.Value == IntPtr.Zero)
148                                 throw new ArgumentException ("The handle is invalid.");
149                         return internal_from_handle_type (handle.Value, IntPtr.Zero);
150                 }
151
152                 [ComVisible (false)]
153                 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
154                 {
155                         if (handle.Value == IntPtr.Zero)
156                                 throw new ArgumentException ("The handle is invalid.");
157                         FieldInfo fi = internal_from_handle_type (handle.Value, declaringType.Value);
158                         if (fi == null)
159                                 throw new ArgumentException ("The field handle and the type handle are incompatible.");
160                         return fi;
161                 }
162
163                 //
164                 // Note: making this abstract imposes an implementation requirement
165                 //       on any class that derives from it.  However, since it's also
166                 //       internal, that means only classes inside corlib can derive
167                 //       from FieldInfo.  See
168                 //
169                 //          errors/cs0534-4.cs errors/CS0534-4-lib.cs
170                 //
171                 //          class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
172                 //
173                 internal virtual int GetFieldOffset ()
174                 {
175                         throw new SystemException ("This method should not be called");
176                 }
177
178                 [CLSCompliant(false)]
179                 [MonoTODO("Not implemented")]
180                 public virtual object GetValueDirect (TypedReference obj)
181                 {
182                         throw new NotImplementedException ();
183                 }
184
185                 [CLSCompliant(false)]
186                 [MonoTODO("Not implemented")]
187                 public virtual void SetValueDirect (TypedReference obj, object value)
188                 {
189                         throw new NotImplementedException ();
190                 }
191
192                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
193                 private extern MarshalAsAttribute get_marshal_info ();
194
195                 internal object[] GetPseudoCustomAttributes ()
196                 {
197                         int count = 0;
198
199                         if (IsNotSerialized)
200                                 count ++;
201
202                         if (DeclaringType.IsExplicitLayout)
203                                 count ++;
204
205                         MarshalAsAttribute marshalAs = get_marshal_info ();
206                         if (marshalAs != null)
207                                 count ++;
208
209                         if (count == 0)
210                                 return null;
211                         object[] attrs = new object [count];
212                         count = 0;
213
214                         if (IsNotSerialized)
215                                 attrs [count ++] = new NonSerializedAttribute ();
216                         if (DeclaringType.IsExplicitLayout)
217                                 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
218                         if (marshalAs != null)
219                                 attrs [count ++] = marshalAs;
220
221                         return attrs;
222                 }
223
224                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
225                 extern Type[] GetTypeModifiers (bool optional);
226
227                 public virtual Type[] GetOptionalCustomModifiers () {
228                         Type[] types = GetTypeModifiers (true);
229                         if (types == null)
230                                 return Type.EmptyTypes;
231                         return types;
232                 }
233
234                 public virtual Type[] GetRequiredCustomModifiers () {
235                         Type[] types = GetTypeModifiers (false);
236                         if (types == null)
237                                 return Type.EmptyTypes;
238                         return types;
239                 }
240
241                 public virtual object GetRawConstantValue ()
242                 {
243                         throw new NotSupportedException ("This non-CLS method is not implemented.");
244                 }
245
246
247 #if NET_4_0
248                 public override bool Equals (object obj)
249                 {
250                         return obj == (object) this;
251                 }
252
253                 public override int GetHashCode ()
254                 {
255                         return base.GetHashCode ();
256                 }
257
258                 public static bool operator == (FieldInfo left, FieldInfo right)
259                 {
260                         if ((object)left == (object)right)
261                                 return true;
262                         if ((object)left == null ^ (object)right == null)
263                                 return false;
264                         return left.Equals (right);
265                 }
266
267                 public static bool operator != (FieldInfo left, FieldInfo right)
268                 {
269                         if ((object)left == (object)right)
270                                 return false;
271                         if ((object)left == null ^ (object)right == null)
272                                 return true;
273                         return !left.Equals (right);
274                 }
275                 
276                 public virtual bool IsSecurityCritical {
277                         get {
278                                 throw new NotImplementedException ();
279                         }
280                 }
281                 
282                 public virtual bool IsSecuritySafeCritical {
283                         get {
284                                 throw new NotImplementedException ();
285                         }
286                 }
287
288                 public virtual bool IsSecurityTransparent {
289                         get {
290                                 throw new NotImplementedException ();
291                         }
292                 }
293 #endif
294                 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
295                 {
296                         throw new NotImplementedException ();
297                 }
298
299                 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
300                 {
301                         throw new NotImplementedException ();
302                 }
303
304                 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
305                 {
306                         throw new NotImplementedException ();
307                 }
308
309                 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
310                 {
311                         throw new NotImplementedException ();
312                 }
313         }
314 }