2004-01-04 Nick Drochak <ndrochak@gol.com>
[mono.git] / mcs / class / corlib / System.Reflection / MonoField.cs
1
2 //
3 // System.Reflection/MonoField.cs
4 // The class used to represent Fields from the mono runtime.
5 //
6 // Author:
7 //   Paolo Molaro (lupus@ximian.com)
8 //
9 // (C) 2001 Ximian, Inc.  http://www.ximian.com
10 //
11
12 using System;
13 using System.Globalization;
14 using System.Runtime.CompilerServices;
15 using System.Runtime.InteropServices;
16
17 namespace System.Reflection {
18         
19         internal class MonoField : FieldInfo {
20                 internal IntPtr klass;
21                 internal RuntimeFieldHandle fhandle;
22                 string name;
23                 Type type;
24                 FieldAttributes attrs;
25                 
26                 public override FieldAttributes Attributes {
27                         get {
28                                 return attrs;
29                         }
30                 }
31                 public override RuntimeFieldHandle FieldHandle {
32                         get {
33                                 return fhandle;
34                         }
35                 }
36
37                 public override Type FieldType { 
38                         get {
39                                 return type;
40                         }
41                 }
42
43                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
44                 private extern Type GetParentType (bool declaring);
45
46                 public override Type ReflectedType {
47                         get {
48                                 return GetParentType (false);
49                         }
50                 }
51                 public override Type DeclaringType {
52                         get {
53                                 return GetParentType (true);
54                         }
55                 }
56                 public override string Name {
57                         get {
58                                 return name;
59                         }
60                 }
61
62                 public override bool IsDefined (Type attributeType, bool inherit) {
63                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
64                 }
65
66                 public override object[] GetCustomAttributes( bool inherit) {
67                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
68                 }
69                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
70                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
71                 }
72
73                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
74                 private extern object GetValueInternal (object obj);
75
76                 public override object GetValue (object obj)
77                 {
78                         return GetValueInternal (obj);
79                 }
80
81                 public override string ToString () {
82                         return String.Format ("{0} {1}", type, name);
83                 }
84
85                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
86                 private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
87
88                 public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
89                 {
90                         if (!IsStatic && obj == null)
91                                 throw new ArgumentNullException ("obj");
92                         if (binder == null)
93                                 binder = Binder.DefaultBinder;
94                         if (val != null) {
95                                 val = binder.ChangeType (val, type, culture);
96                                 if (val == null)
97                                         throw new ArgumentException ("Object type cannot be converted to target type.", "val");
98                         }
99                         SetValueInternal (this, obj, val);
100                 }
101         }
102 }