[bcl] Remove NET_4_0 defines from class libs.
[mono.git] / mcs / class / corlib / System.Reflection / MonoField.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24
25 //
26 // System.Reflection/MonoField.cs
27 // The class used to represent Fields from the mono runtime.
28 //
29 // Author:
30 //   Paolo Molaro (lupus@ximian.com)
31 //
32 // (C) 2001 Ximian, Inc.  http://www.ximian.com
33 //
34
35 using System;
36 using System.Collections.Generic;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Runtime.Serialization;
41
42 namespace System.Reflection {
43
44         [Serializable]
45         [StructLayout (LayoutKind.Sequential)]
46         internal class MonoField : FieldInfo, ISerializable {
47                 internal IntPtr klass;
48                 internal RuntimeFieldHandle fhandle;
49                 string name;
50                 Type type;
51                 FieldAttributes attrs;
52                 
53                 public override FieldAttributes Attributes {
54                         get {
55                                 return attrs;
56                         }
57                 }
58                 public override RuntimeFieldHandle FieldHandle {
59                         get {
60                                 return fhandle;
61                         }
62                 }
63
64                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
65                 extern Type ResolveType ();
66
67                 public override Type FieldType { 
68                         get {
69                                 if (type == null)
70                                         type = ResolveType ();
71                                 return type;
72                         }
73                 }
74
75                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
76                 private extern Type GetParentType (bool declaring);
77
78                 public override Type ReflectedType {
79                         get {
80                                 return GetParentType (false);
81                         }
82                 }
83                 public override Type DeclaringType {
84                         get {
85                                 return GetParentType (true);
86                         }
87                 }
88                 public override string Name {
89                         get {
90                                 return name;
91                         }
92                 }
93
94                 public override bool IsDefined (Type attributeType, bool inherit) {
95                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
96                 }
97
98                 public override object[] GetCustomAttributes( bool inherit) {
99                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
100                 }
101                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
102                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
103                 }
104
105                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
106                 internal override extern int GetFieldOffset ();
107
108                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
109                 private extern object GetValueInternal (object obj);
110
111                 public override object GetValue (object obj)
112                 {
113                         if (!IsStatic) {
114                                 if (obj == null)
115                                         throw new TargetException ("Non-static field requires a target");
116                                 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
117                                         throw new ArgumentException (string.Format (
118                                                 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
119                                                 Name, DeclaringType, obj.GetType ()),
120                                                 "obj");
121                         }
122                         
123                         if (!IsLiteral)
124                                 CheckGeneric ();
125                         return GetValueInternal (obj);
126                 }
127
128                 public override string ToString () {
129                         return String.Format ("{0} {1}", FieldType, name);
130                 }
131
132                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
133                 private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
134
135                 public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
136                 {
137                         if (!IsStatic) {
138                                 if (obj == null)
139                                         throw new TargetException ("Non-static field requires a target");
140                                 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
141                                         throw new ArgumentException (string.Format (
142                                                 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
143                                                 Name, DeclaringType, obj.GetType ()),
144                                                 "obj");
145                         }
146                         if (IsLiteral)
147                                 throw new FieldAccessException ("Cannot set a constant field");
148                         if (binder == null)
149                                 binder = Binder.DefaultBinder;
150                         CheckGeneric ();
151                         if (val != null) {
152                                 val = binder.ConvertValue (val, FieldType, culture, (invokeAttr & BindingFlags.ExactBinding) != 0);
153                         }
154                         SetValueInternal (this, obj, val);
155                 }
156                 
157                 internal MonoField Clone (string newName)
158                 {
159                         MonoField field = new MonoField ();
160                         field.name = newName;
161                         field.type = type;
162                         field.attrs = attrs;
163                         field.klass = klass;
164                         field.fhandle = fhandle;
165                         return field;
166                 }
167
168                 // ISerializable
169                 public void GetObjectData (SerializationInfo info, StreamingContext context) 
170                 {
171                         MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
172                                 ToString(), MemberTypes.Field);
173                 }
174
175                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
176                 public override extern object GetRawConstantValue ();
177
178                 public override IList<CustomAttributeData> GetCustomAttributesData () {
179                         return CustomAttributeData.GetCustomAttributes (this);
180                 }
181
182                 void CheckGeneric () {
183                         if (DeclaringType.ContainsGenericParameters)
184                                 throw new InvalidOperationException ("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.");
185             }
186         }
187 }