2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / corlib / System.Reflection / MonoField.cs
blobdf0becc0f61dac3dcc642cd7b66ff309d01d63a3
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.
26 // System.Reflection/MonoField.cs
27 // The class used to represent Fields from the mono runtime.
29 // Author:
30 // Paolo Molaro (lupus@ximian.com)
32 // (C) 2001 Ximian, Inc. http://www.ximian.com
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;
42 namespace System.Reflection {
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;
52 public override FieldAttributes Attributes {
53 get {
54 return attrs;
57 public override RuntimeFieldHandle FieldHandle {
58 get {
59 return fhandle;
63 public override Type FieldType {
64 get {
65 return type;
69 [MethodImplAttribute(MethodImplOptions.InternalCall)]
70 private extern Type GetParentType (bool declaring);
72 public override Type ReflectedType {
73 get {
74 return GetParentType (false);
77 public override Type DeclaringType {
78 get {
79 return GetParentType (true);
82 public override string Name {
83 get {
84 return name;
88 public override bool IsDefined (Type attributeType, bool inherit) {
89 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
92 public override object[] GetCustomAttributes( bool inherit) {
93 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
95 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
96 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
99 [MethodImplAttribute(MethodImplOptions.InternalCall)]
100 internal override extern int GetFieldOffset ();
102 [MethodImplAttribute(MethodImplOptions.InternalCall)]
103 private extern object GetValueInternal (object obj);
105 public override object GetValue (object obj)
107 if (!IsStatic) {
108 if (obj == null)
109 throw new TargetException ("Non-static field requires a target");
110 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
111 throw new ArgumentException (string.Format (
112 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
113 Name, DeclaringType, obj.GetType ()),
114 "obj");
117 if (!IsLiteral)
118 CheckGeneric ();
119 return GetValueInternal (obj);
122 public override string ToString () {
123 return String.Format ("{0} {1}", type, name);
126 [MethodImplAttribute(MethodImplOptions.InternalCall)]
127 private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
129 public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
131 if (!IsStatic) {
132 if (obj == null)
133 throw new TargetException ("Non-static field requires a target");
134 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
135 throw new ArgumentException (string.Format (
136 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
137 Name, DeclaringType, obj.GetType ()),
138 "obj");
140 if (IsLiteral)
141 throw new FieldAccessException ("Cannot set a constant field");
142 if (binder == null)
143 binder = Binder.DefaultBinder;
144 CheckGeneric ();
145 if (val != null) {
146 object newVal;
147 newVal = binder.ChangeType (val, type, culture);
148 if (newVal == null)
149 throw new ArgumentException ("Object type " + val.GetType() + " cannot be converted to target type: " + type, "val");
150 val = newVal;
152 SetValueInternal (this, obj, val);
155 internal MonoField Clone (string newName)
157 MonoField field = new MonoField ();
158 field.name = newName;
159 field.type = type;
160 field.attrs = attrs;
161 field.klass = klass;
162 field.fhandle = fhandle;
163 return field;
166 // ISerializable
167 public void GetObjectData (SerializationInfo info, StreamingContext context)
169 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
170 ToString(), MemberTypes.Field);
173 [MethodImplAttribute(MethodImplOptions.InternalCall)]
174 public override extern object GetRawConstantValue ();
176 #if NET_4_0
177 public override IList<CustomAttributeData> GetCustomAttributesData () {
178 return CustomAttributeData.GetCustomAttributes (this);
180 #endif
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.");