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