Merge pull request #1325 from madrang/CryptoToolsCtorCheck
[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 #if MOBILE
42         public abstract class FieldInfo : MemberInfo {
43 #else
44         public abstract class FieldInfo : MemberInfo, _FieldInfo {
45 #endif
46                 public abstract FieldAttributes Attributes {get;}
47                 public abstract RuntimeFieldHandle FieldHandle {get;}
48
49                 protected FieldInfo () {}
50                 
51                 public abstract Type FieldType { get; }
52
53                 public abstract object GetValue(object obj);
54
55                 public override MemberTypes MemberType {
56                         get { return MemberTypes.Field;}
57                 }
58
59                 public bool IsLiteral
60                 {
61                         get {return (Attributes & FieldAttributes.Literal) != 0;}
62                 } 
63
64                 public bool IsStatic
65                 {
66                         get {return (Attributes & FieldAttributes.Static) != 0;}
67                 } 
68
69                 public bool IsInitOnly
70                 {
71                         get {return (Attributes & FieldAttributes.InitOnly) != 0;}
72                 } 
73                 public Boolean IsPublic
74                 { 
75                         get
76                         {
77                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
78                         }
79                 }
80                 public Boolean IsPrivate
81                 {
82                         get
83                         {
84                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
85                         }
86                 }
87                 public Boolean IsFamily
88                 {
89                         get
90                         {
91                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
92                         }
93                 }
94                 public Boolean IsAssembly
95                 {
96                         get
97                         {
98                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
99                         }
100                 }
101                 public Boolean IsFamilyAndAssembly
102                 {
103                         get {
104                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
105                         }
106                 }
107                 public Boolean IsFamilyOrAssembly
108                 {
109                         get
110                         {
111                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
112                         }
113                 }
114                 public Boolean IsPinvokeImpl
115                 {
116                         get
117                         {
118                                 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
119                         }
120                 }
121                 public Boolean IsSpecialName
122                 {
123                         get
124                         {
125                                 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
126                         }
127                 }
128                 public Boolean IsNotSerialized
129                 {
130                         get
131                         {
132                                 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
133                         }
134                 }
135
136                 public abstract void SetValue (object obj, object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
137
138                 [DebuggerHidden]
139                 [DebuggerStepThrough]
140                 public void SetValue (object obj, object value)
141                 {
142                         SetValue (obj, value, 0, null, null);
143                 }
144
145                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
146                 private static extern FieldInfo internal_from_handle_type (IntPtr field_handle, IntPtr type_handle);
147
148                 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
149                 {
150                         if (handle.Value == IntPtr.Zero)
151                                 throw new ArgumentException ("The handle is invalid.");
152                         return internal_from_handle_type (handle.Value, IntPtr.Zero);
153                 }
154
155                 [ComVisible (false)]
156                 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle, RuntimeTypeHandle declaringType)
157                 {
158                         if (handle.Value == IntPtr.Zero)
159                                 throw new ArgumentException ("The handle is invalid.");
160                         FieldInfo fi = internal_from_handle_type (handle.Value, declaringType.Value);
161                         if (fi == null)
162                                 throw new ArgumentException ("The field handle and the type handle are incompatible.");
163                         return fi;
164                 }
165
166                 //
167                 // Note: making this abstract imposes an implementation requirement
168                 //       on any class that derives from it.  However, since it's also
169                 //       internal, that means only classes inside corlib can derive
170                 //       from FieldInfo.  See
171                 //
172                 //          errors/cs0534-4.cs errors/CS0534-4-lib.cs
173                 //
174                 //          class/Microsoft.JScript/Microsoft.JScript/JSFieldInfo.cs
175                 //
176                 internal virtual int GetFieldOffset ()
177                 {
178                         throw new SystemException ("This method should not be called");
179                 }
180
181                 [CLSCompliant(false)]
182                 public virtual object GetValueDirect (TypedReference obj)
183                 {
184                         throw new NotSupportedException(Environment.GetResourceString("NotSupported_AbstractNonCLS"));
185                 }
186
187                 [CLSCompliant(false)]
188                 public virtual void SetValueDirect (TypedReference obj, object value)
189                 {
190                         throw new NotSupportedException(Environment.GetResourceString("NotSupported_AbstractNonCLS"));
191                 }
192
193                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
194                 private extern MarshalAsAttribute get_marshal_info ();
195
196                 internal object[] GetPseudoCustomAttributes ()
197                 {
198                         int count = 0;
199
200                         if (IsNotSerialized)
201                                 count ++;
202
203                         if (DeclaringType.IsExplicitLayout)
204                                 count ++;
205
206                         MarshalAsAttribute marshalAs = get_marshal_info ();
207                         if (marshalAs != null)
208                                 count ++;
209
210                         if (count == 0)
211                                 return null;
212                         object[] attrs = new object [count];
213                         count = 0;
214
215                         if (IsNotSerialized)
216                                 attrs [count ++] = new NonSerializedAttribute ();
217                         if (DeclaringType.IsExplicitLayout)
218                                 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
219                         if (marshalAs != null)
220                                 attrs [count ++] = marshalAs;
221
222                         return attrs;
223                 }
224
225                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
226                 extern Type[] GetTypeModifiers (bool optional);
227
228                 public virtual Type[] GetOptionalCustomModifiers () {
229                         Type[] types = GetTypeModifiers (true);
230                         if (types == null)
231                                 return Type.EmptyTypes;
232                         return types;
233                 }
234
235                 public virtual Type[] GetRequiredCustomModifiers () {
236                         Type[] types = GetTypeModifiers (false);
237                         if (types == null)
238                                 return Type.EmptyTypes;
239                         return types;
240                 }
241
242                 public virtual object GetRawConstantValue ()
243                 {
244                         throw new NotSupportedException ("This non-CLS method is not implemented.");
245                 }
246
247
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 NotSupportedException ();
279                         }
280                 }
281                 
282                 public virtual bool IsSecuritySafeCritical {
283                         get {
284                                 throw new NotSupportedException ();
285                         }
286                 }
287
288                 public virtual bool IsSecurityTransparent {
289                         get {
290                                 throw new NotSupportedException ();
291                         }
292                 }
293
294 #if !MOBILE
295                 void _FieldInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
296                 {
297                         throw new NotImplementedException ();
298                 }
299
300                 Type _FieldInfo.GetType ()
301                 {
302                         // Required or object::GetType becomes virtual final
303                         return base.GetType ();
304                 }
305
306                 void _FieldInfo.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
307                 {
308                         throw new NotImplementedException ();
309                 }
310
311                 void _FieldInfo.GetTypeInfoCount (out uint pcTInfo)
312                 {
313                         throw new NotImplementedException ();
314                 }
315
316                 void _FieldInfo.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
317                 {
318                         throw new NotImplementedException ();
319                 }
320 #endif
321         }
322 }