3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
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.
30 // Paolo Molaro (lupus@ximian.com)
32 // (C) 2001 Ximian, Inc. http://www.ximian.com
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 abstract class RuntimeFieldInfo : FieldInfo
46 internal BindingFlags BindingFlags {
53 abstract class RtFieldInfo : RuntimeFieldInfo
55 [MethodImplAttribute(MethodImplOptions.InternalCall)]
56 internal extern object UnsafeGetValue (object obj);
60 [StructLayout (LayoutKind.Sequential)]
61 internal class MonoField : RtFieldInfo, ISerializable {
62 internal IntPtr klass;
63 internal RuntimeFieldHandle fhandle;
66 FieldAttributes attrs;
68 public override FieldAttributes Attributes {
73 public override RuntimeFieldHandle FieldHandle {
79 [MethodImplAttribute(MethodImplOptions.InternalCall)]
80 extern Type ResolveType ();
82 public override Type FieldType {
85 type = ResolveType ();
90 [MethodImplAttribute(MethodImplOptions.InternalCall)]
91 private extern Type GetParentType (bool declaring);
93 public override Type ReflectedType {
95 return GetParentType (false);
98 public override Type DeclaringType {
100 return GetParentType (true);
103 public override string Name {
109 public override bool IsDefined (Type attributeType, bool inherit) {
110 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
113 public override object[] GetCustomAttributes( bool inherit) {
114 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
116 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
117 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
120 [MethodImplAttribute(MethodImplOptions.InternalCall)]
121 internal override extern int GetFieldOffset ();
123 [MethodImplAttribute(MethodImplOptions.InternalCall)]
124 private extern object GetValueInternal (object obj);
126 public override object GetValue (object obj)
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 ()),
140 return GetValueInternal (obj);
143 public override string ToString () {
144 return String.Format ("{0} {1}", FieldType, name);
147 [MethodImplAttribute(MethodImplOptions.InternalCall)]
148 private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
150 public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
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 ()),
162 throw new FieldAccessException ("Cannot set a constant field");
164 binder = Type.DefaultBinder;
167 RuntimeType fieldType = (RuntimeType) FieldType;
168 val = fieldType.CheckValue (val, binder, culture, invokeAttr);
170 SetValueInternal (this, obj, val);
173 internal MonoField Clone (string newName)
175 MonoField field = new MonoField ();
176 field.name = newName;
180 field.fhandle = fhandle;
185 public void GetObjectData (SerializationInfo info, StreamingContext context)
187 MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
188 ToString(), MemberTypes.Field);
191 [MethodImplAttribute(MethodImplOptions.InternalCall)]
192 public override extern object GetRawConstantValue ();
194 public override IList<CustomAttributeData> GetCustomAttributesData () {
195 return CustomAttributeData.GetCustomAttributes (this);
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.");