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