[runtime] Updates comments.
[mono.git] / mcs / class / corlib / System.Reflection / MonoField.cs
index 986be089ddc79db1c793ed359d0c4239ec70d7e5..117eea363fd520e7c1998c4aedd28d2f0f580a37 100644 (file)
 //
 
 using System;
+using System.Collections.Generic;
 using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
+using System.Runtime.Serialization;
 
 namespace System.Reflection {
-       
-       internal class MonoField : FieldInfo {
+
+       abstract class RuntimeFieldInfo : FieldInfo
+       {
+               internal BindingFlags BindingFlags {
+                       get {
+                               return 0;
+                       }
+               }
+       }
+
+       abstract class RtFieldInfo : RuntimeFieldInfo
+       {
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               internal extern object UnsafeGetValue (object obj);
+       }
+
+       [Serializable]
+       [StructLayout (LayoutKind.Sequential)]
+       internal class MonoField : RtFieldInfo, ISerializable {
                internal IntPtr klass;
                internal RuntimeFieldHandle fhandle;
                string name;
@@ -57,8 +76,13 @@ namespace System.Reflection {
                        }
                }
 
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               extern Type ResolveType ();
+
                public override Type FieldType { 
                        get {
+                               if (type == null)
+                                       type = ResolveType ();
                                return type;
                        }
                }
@@ -101,11 +125,23 @@ namespace System.Reflection {
 
                public override object GetValue (object obj)
                {
+                       if (!IsStatic) {
+                               if (obj == null)
+                                       throw new TargetException ("Non-static field requires a target");
+                               if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
+                                       throw new ArgumentException (string.Format (
+                                               "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
+                                               Name, DeclaringType, obj.GetType ()),
+                                               "obj");
+                       }
+                       
+                       if (!IsLiteral)
+                               CheckGeneric ();
                        return GetValueInternal (obj);
                }
 
                public override string ToString () {
-                       return String.Format ("{0} {1}", type, name);
+                       return String.Format ("{0} {1}", FieldType, name);
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -113,37 +149,55 @@ namespace System.Reflection {
 
                public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
                {
-                       if (!IsStatic && obj == null)
-                               throw new ArgumentNullException ("obj");
+                       if (!IsStatic) {
+                               if (obj == null)
+                                       throw new TargetException ("Non-static field requires a target");
+                               if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
+                                       throw new ArgumentException (string.Format (
+                                               "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
+                                               Name, DeclaringType, obj.GetType ()),
+                                               "obj");
+                       }
+                       if (IsLiteral)
+                               throw new FieldAccessException ("Cannot set a constant field");
                        if (binder == null)
-                               binder = Binder.DefaultBinder;
+                               binder = Type.DefaultBinder;
+                       CheckGeneric ();
                        if (val != null) {
-                               val = binder.ChangeType (val, type, culture);
-                               if (val == null)
-                                       throw new ArgumentException ("Object type cannot be converted to target type.", "val");
+                               RuntimeType fieldType = (RuntimeType) FieldType;
+                               val = fieldType.CheckValue (val, binder, culture, invokeAttr);
                        }
                        SetValueInternal (this, obj, val);
                }
-
-#if NET_2_0 || BOOTSTRAP_NET_2_0
-               [MonoTODO]
-               public override Type[] OptionalCustomModifiers {
-                       get {
-                               throw new NotImplementedException ();
-                       }
+               
+               internal MonoField Clone (string newName)
+               {
+                       MonoField field = new MonoField ();
+                       field.name = newName;
+                       field.type = type;
+                       field.attrs = attrs;
+                       field.klass = klass;
+                       field.fhandle = fhandle;
+                       return field;
                }
 
-               [MonoTODO]
-               public override Type[] RequiredCustomModifiers {
-                       get {
-                               throw new NotImplementedException ();
-                       }
+               // ISerializable
+               public void GetObjectData (SerializationInfo info, StreamingContext context) 
+               {
+                       MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
+                               ToString(), MemberTypes.Field);
                }
-#endif
 
-#if NET_2_0 || BOOTSTRAP_NET_2_0
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               public override extern FieldInfo Mono_GetGenericFieldDefinition ();
-#endif
+               public override extern object GetRawConstantValue ();
+
+               public override IList<CustomAttributeData> GetCustomAttributesData () {
+                       return CustomAttributeData.GetCustomAttributes (this);
+               }
+
+               void CheckGeneric () {
+                       if (DeclaringType.ContainsGenericParameters)
+                               throw new InvalidOperationException ("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.");
+           }
        }
 }