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