Merge branch 'master' of github.com:mono/mono
[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         internal class MonoField : FieldInfo, ISerializable {
46                 internal IntPtr klass;
47                 internal RuntimeFieldHandle fhandle;
48                 string name;
49                 Type type;
50                 FieldAttributes attrs;
51                 
52                 public override FieldAttributes Attributes {
53                         get {
54                                 return attrs;
55                         }
56                 }
57                 public override RuntimeFieldHandle FieldHandle {
58                         get {
59                                 return fhandle;
60                         }
61                 }
62
63                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
64                 extern Type ResolveType ();
65
66                 public override Type FieldType { 
67                         get {
68                                 if (type == null)
69                                         type = ResolveType ();
70                                 return type;
71                         }
72                 }
73
74                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
75                 private extern Type GetParentType (bool declaring);
76
77                 public override Type ReflectedType {
78                         get {
79                                 return GetParentType (false);
80                         }
81                 }
82                 public override Type DeclaringType {
83                         get {
84                                 return GetParentType (true);
85                         }
86                 }
87                 public override string Name {
88                         get {
89                                 return name;
90                         }
91                 }
92
93                 public override bool IsDefined (Type attributeType, bool inherit) {
94                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
95                 }
96
97                 public override object[] GetCustomAttributes( bool inherit) {
98                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
99                 }
100                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
101                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
102                 }
103
104                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
105                 internal override extern int GetFieldOffset ();
106
107                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108                 private extern object GetValueInternal (object obj);
109
110                 public override object GetValue (object obj)
111                 {
112                         if (!IsStatic) {
113                                 if (obj == null)
114                                         throw new TargetException ("Non-static field requires a target");
115                                 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
116                                         throw new ArgumentException (string.Format (
117                                                 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
118                                                 Name, DeclaringType, obj.GetType ()),
119                                                 "obj");
120                         }
121                         
122                         if (!IsLiteral)
123                                 CheckGeneric ();
124                         return GetValueInternal (obj);
125                 }
126
127                 public override string ToString () {
128                         return String.Format ("{0} {1}", FieldType, name);
129                 }
130
131                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
132                 private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
133
134                 public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
135                 {
136                         if (!IsStatic) {
137                                 if (obj == null)
138                                         throw new TargetException ("Non-static field requires a target");
139                                 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
140                                         throw new ArgumentException (string.Format (
141                                                 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
142                                                 Name, DeclaringType, obj.GetType ()),
143                                                 "obj");
144                         }
145                         if (IsLiteral)
146                                 throw new FieldAccessException ("Cannot set a constant field");
147                         if (binder == null)
148                                 binder = Binder.DefaultBinder;
149                         CheckGeneric ();
150                         if (val != null) {
151                                 object newVal;
152                                 newVal = binder.ChangeType (val, FieldType, culture);
153                                 if (newVal == null)
154                                         throw new ArgumentException ("Object type " + val.GetType() + " cannot be converted to target type: " + FieldType, "val");
155                                 val = newVal;
156                         }
157                         SetValueInternal (this, obj, val);
158                 }
159                 
160                 internal MonoField Clone (string newName)
161                 {
162                         MonoField field = new MonoField ();
163                         field.name = newName;
164                         field.type = type;
165                         field.attrs = attrs;
166                         field.klass = klass;
167                         field.fhandle = fhandle;
168                         return field;
169                 }
170
171                 // ISerializable
172                 public void GetObjectData (SerializationInfo info, StreamingContext context) 
173                 {
174                         MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
175                                 ToString(), MemberTypes.Field);
176                 }
177
178                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
179                 public override extern object GetRawConstantValue ();
180
181 #if NET_4_0
182                 public override IList<CustomAttributeData> GetCustomAttributesData () {
183                         return CustomAttributeData.GetCustomAttributes (this);
184                 }
185 #endif
186
187                 void CheckGeneric () {
188                         if (DeclaringType.ContainsGenericParameters)
189                                 throw new InvalidOperationException ("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.");
190             }
191         }
192 }