* roottypes.cs: Rename from tree.cs.
[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.Globalization;
37 using System.Runtime.CompilerServices;
38 using System.Runtime.InteropServices;
39
40 namespace System.Reflection {
41         
42         internal class MonoField : FieldInfo {
43                 internal IntPtr klass;
44                 internal RuntimeFieldHandle fhandle;
45                 string name;
46                 Type type;
47                 FieldAttributes attrs;
48                 
49                 public override FieldAttributes Attributes {
50                         get {
51                                 return attrs;
52                         }
53                 }
54                 public override RuntimeFieldHandle FieldHandle {
55                         get {
56                                 return fhandle;
57                         }
58                 }
59
60                 public override Type FieldType { 
61                         get {
62                                 return type;
63                         }
64                 }
65
66                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
67                 private extern Type GetParentType (bool declaring);
68
69                 public override Type ReflectedType {
70                         get {
71                                 return GetParentType (false);
72                         }
73                 }
74                 public override Type DeclaringType {
75                         get {
76                                 return GetParentType (true);
77                         }
78                 }
79                 public override string Name {
80                         get {
81                                 return name;
82                         }
83                 }
84
85                 public override bool IsDefined (Type attributeType, bool inherit) {
86                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
87                 }
88
89                 public override object[] GetCustomAttributes( bool inherit) {
90                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
91                 }
92                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
93                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
94                 }
95
96                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
97                 internal override extern int GetFieldOffset ();
98
99                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
100                 private extern object GetValueInternal (object obj);
101
102                 public override object GetValue (object obj)
103                 {
104                         if (!IsStatic && obj == null)
105                                 throw new TargetException ("Non-static field requires a target");
106                         return GetValueInternal (obj);
107                 }
108
109                 public override string ToString () {
110                         return String.Format ("{0} {1}", type, name);
111                 }
112
113                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
114                 private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
115
116                 public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
117                 {
118                         if (!IsStatic && obj == null)
119                                 throw new TargetException ("Non-static field requires a target");
120                         if (binder == null)
121                                 binder = Binder.DefaultBinder;
122                         if (val != null) {
123                                 object newVal;
124                                 newVal = binder.ChangeType (val, type, culture);
125                                 if (newVal == null)
126                                         throw new ArgumentException ("Object type " + val.GetType() + " cannot be converted to target type: " + type, "val");
127                                 val = newVal;
128                         }
129                         SetValueInternal (this, obj, val);
130                 }
131                 
132                 internal MonoField Clone (string newName)
133                 {
134                         MonoField field = new MonoField ();
135                         field.name = newName;
136                         field.type = type;
137                         field.attrs = attrs;
138                         field.klass = klass;
139                         field.fhandle = fhandle;
140                         return field;
141                 }
142         }
143 }