Copied remotely
[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 //
9 // TODO: Mucho left to implement.
10 //
11
12 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 // 
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 // 
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 //
34
35 using System;
36 using System.Diagnostics;
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Globalization;
40 using System.Runtime.CompilerServices;
41 using System.Runtime.InteropServices;
42
43 namespace System.Reflection {
44
45         [Serializable]
46         [ClassInterface(ClassInterfaceType.AutoDual)]
47         public abstract class FieldInfo : MemberInfo {
48                 public abstract FieldAttributes Attributes {get;}
49                 public abstract RuntimeFieldHandle FieldHandle {get;}
50
51                 protected FieldInfo () {}
52                 
53                 public abstract Type FieldType { get; }
54
55                 public abstract object GetValue(object obj);
56
57                 public override MemberTypes MemberType {
58                         get { return MemberTypes.Field;}
59                 }
60
61                 public bool IsLiteral
62                 {
63                         get {return (Attributes & FieldAttributes.Literal) != 0;}
64                 } 
65
66                 public bool IsStatic
67                 {
68                         get {return (Attributes & FieldAttributes.Static) != 0;}
69                 } 
70
71                 public bool IsInitOnly
72                 {
73                         get {return (Attributes & FieldAttributes.InitOnly) != 0;}
74                 } 
75                 public Boolean IsPublic
76                 { 
77                         get
78                         {
79                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Public;
80                         }
81                 }
82                 public Boolean IsPrivate
83                 {
84                         get
85                         {
86                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Private;
87                         }
88                 }
89                 public Boolean IsFamily
90                 {
91                         get
92                         {
93                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Family;
94                         }
95                 }
96                 public Boolean IsAssembly
97                 {
98                         get
99                         {
100                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.Assembly;
101                         }
102                 }
103                 public Boolean IsFamilyAndAssembly
104                 {
105                         get {
106                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamANDAssem;
107                         }
108                 }
109                 public Boolean IsFamilyOrAssembly
110                 {
111                         get
112                         {
113                                 return (Attributes & FieldAttributes.FieldAccessMask) == FieldAttributes.FamORAssem;
114                         }
115                 }
116                 public Boolean IsPinvokeImpl
117                 {
118                         get
119                         {
120                                 return (Attributes & FieldAttributes.PinvokeImpl) == FieldAttributes.PinvokeImpl;
121                         }
122                 }
123                 public Boolean IsSpecialName
124                 {
125                         get
126                         {
127                                 return (Attributes & FieldAttributes.SpecialName) == FieldAttributes.SpecialName;
128                         }
129                 }
130                 public Boolean IsNotSerialized
131                 {
132                         get
133                         {
134                                 return (Attributes & FieldAttributes.NotSerialized) == FieldAttributes.NotSerialized;
135                         }
136                 }
137
138                 public abstract void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture);
139
140                 [DebuggerHidden]
141                 [DebuggerStepThrough]
142                 public void SetValue (object obj, object value)
143                 {
144                         SetValue (obj, value, 0, null, null);
145                 }
146
147                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
148                 private static extern FieldInfo internal_from_handle (IntPtr handle);
149
150                 public static FieldInfo GetFieldFromHandle (RuntimeFieldHandle handle)
151                 {
152                         return internal_from_handle (handle.Value);
153                 }
154
155                 internal abstract int GetFieldOffset ();
156
157                 [CLSCompliant(false)]
158                 [MonoTODO]
159                 public virtual object GetValueDirect (TypedReference obj)
160                 {
161                         throw new NotImplementedException ();
162                 }
163
164                 [CLSCompliant(false)]
165                 [MonoTODO]
166                 public virtual void SetValueDirect (TypedReference obj, object value)
167                 {
168                         throw new NotImplementedException ();
169                 }
170
171                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
172                 private extern UnmanagedMarshal GetUnmanagedMarshal ();
173
174                 internal object[] GetPseudoCustomAttributes ()
175                 {
176                         int count = 0;
177
178                         if (IsNotSerialized)
179                                 count ++;
180
181                         if (DeclaringType.IsExplicitLayout)
182                                 count ++;
183
184                         UnmanagedMarshal marshalAs = GetUnmanagedMarshal ();
185                         if (marshalAs != null)
186                                 count ++;
187
188                         if (count == 0)
189                                 return null;
190                         object[] attrs = new object [count];
191                         count = 0;
192
193                         if (IsNotSerialized)
194                                 attrs [count ++] = new NonSerializedAttribute ();
195                         if (DeclaringType.IsExplicitLayout)
196                                 attrs [count ++] = new FieldOffsetAttribute (GetFieldOffset ());
197                         if (marshalAs != null)
198                                 attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
199
200                         return attrs;
201                 }
202
203 #if NET_2_0 || BOOTSTRAP_NET_2_0
204                 public virtual Type[] OptionalCustomModifiers {
205                         get {
206                                 throw new NotImplementedException ();
207                         }
208                 }
209
210                 public virtual Type[] RequiredCustomModifiers {
211                         get {
212                                 throw new NotImplementedException ();
213                         }
214                 }
215 #endif
216
217 #if NET_2_0 || BOOTSTRAP_NET_2_0
218                 public abstract FieldInfo Mono_GetGenericFieldDefinition ();
219 #endif
220         }
221 }