[profiler] Redesign buffer flushing and method reporting.
[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.Collections.Generic;
37 using System.Globalization;
38 using System.Runtime.CompilerServices;
39 using System.Runtime.InteropServices;
40 using System.Runtime.Serialization;
41 using System.Diagnostics;
42
43 namespace System.Reflection {
44
45         abstract class RuntimeFieldInfo : FieldInfo
46         {
47                 internal BindingFlags BindingFlags {
48                         get {
49                                 return 0;
50                         }
51                 }
52         }
53
54         abstract class RtFieldInfo : RuntimeFieldInfo
55         {
56                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57                 internal extern object UnsafeGetValue (object obj);
58
59         internal void CheckConsistency(Object target)
60         {
61             // only test instance fields
62             if ((Attributes & FieldAttributes.Static) != FieldAttributes.Static)
63             {
64                 if (!DeclaringType.IsInstanceOfType(target))
65                 {
66                     if (target == null)
67                     {
68 #if FEATURE_LEGACYNETCF
69                         if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
70                             throw new ArgumentNullException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
71                         else
72 #endif
73                         throw new TargetException(Environment.GetResourceString("RFLCT.Targ_StatFldReqTarg"));
74                     }
75                     else
76                     {
77                         throw new ArgumentException(
78                             String.Format(CultureInfo.CurrentUICulture, Environment.GetResourceString("Arg_FieldDeclTarget"),
79                                 Name, DeclaringType, target.GetType()));
80                     }
81                 }
82             }
83         }
84
85                 [DebuggerStepThroughAttribute]
86                 [Diagnostics.DebuggerHidden]
87                 internal void UnsafeSetValue (Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
88                 {
89                         bool domainInitialized = false;
90                         RuntimeFieldHandle.SetValue (this, obj, value, null, Attributes, null, ref domainInitialized);
91                 }
92         }
93
94         [Serializable]
95         [StructLayout (LayoutKind.Sequential)]
96         internal class MonoField : RtFieldInfo, ISerializable {
97                 internal IntPtr klass;
98                 internal RuntimeFieldHandle fhandle;
99                 string name;
100                 Type type;
101                 FieldAttributes attrs;
102                 
103                 public override FieldAttributes Attributes {
104                         get {
105                                 return attrs;
106                         }
107                 }
108                 public override RuntimeFieldHandle FieldHandle {
109                         get {
110                                 return fhandle;
111                         }
112                 }
113
114                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
115                 extern Type ResolveType ();
116
117                 public override Type FieldType { 
118                         get {
119                                 if (type == null)
120                                         type = ResolveType ();
121                                 return type;
122                         }
123                 }
124
125                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
126                 private extern Type GetParentType (bool declaring);
127
128                 public override Type ReflectedType {
129                         get {
130                                 return GetParentType (false);
131                         }
132                 }
133                 public override Type DeclaringType {
134                         get {
135                                 return GetParentType (true);
136                         }
137                 }
138                 public override string Name {
139                         get {
140                                 return name;
141                         }
142                 }
143
144                 public override bool IsDefined (Type attributeType, bool inherit) {
145                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
146                 }
147
148                 public override object[] GetCustomAttributes( bool inherit) {
149                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
150                 }
151                 public override object[] GetCustomAttributes( Type attributeType, bool inherit) {
152                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
153                 }
154
155                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
156                 internal override extern int GetFieldOffset ();
157
158                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
159                 private extern object GetValueInternal (object obj);
160
161                 public override object GetValue (object obj)
162                 {
163                         if (!IsStatic) {
164                                 if (obj == null)
165                                         throw new TargetException ("Non-static field requires a target");
166                                 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
167                                         throw new ArgumentException (string.Format (
168                                                 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
169                                                 Name, DeclaringType, obj.GetType ()),
170                                                 "obj");
171                         }
172                         
173                         if (!IsLiteral)
174                                 CheckGeneric ();
175                         return GetValueInternal (obj);
176                 }
177
178                 public override string ToString () {
179                         return String.Format ("{0} {1}", FieldType, name);
180                 }
181
182                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
183                 private static extern void SetValueInternal (FieldInfo fi, object obj, object value);
184
185                 public override void SetValue (object obj, object val, BindingFlags invokeAttr, Binder binder, CultureInfo culture)
186                 {
187                         if (!IsStatic) {
188                                 if (obj == null)
189                                         throw new TargetException ("Non-static field requires a target");
190                                 if (!DeclaringType.IsAssignableFrom (obj.GetType ()))
191                                         throw new ArgumentException (string.Format (
192                                                 "Field {0} defined on type {1} is not a field on the target object which is of type {2}.",
193                                                 Name, DeclaringType, obj.GetType ()),
194                                                 "obj");
195                         }
196                         if (IsLiteral)
197                                 throw new FieldAccessException ("Cannot set a constant field");
198                         if (binder == null)
199                                 binder = Type.DefaultBinder;
200                         CheckGeneric ();
201                         if (val != null) {
202                                 RuntimeType fieldType = (RuntimeType) FieldType;
203                                 val = fieldType.CheckValue (val, binder, culture, invokeAttr);
204                         }
205                         SetValueInternal (this, obj, val);
206                 }
207                 
208                 internal MonoField Clone (string newName)
209                 {
210                         MonoField field = new MonoField ();
211                         field.name = newName;
212                         field.type = type;
213                         field.attrs = attrs;
214                         field.klass = klass;
215                         field.fhandle = fhandle;
216                         return field;
217                 }
218
219                 // ISerializable
220                 public void GetObjectData (SerializationInfo info, StreamingContext context) 
221                 {
222                         MemberInfoSerializationHolder.Serialize (info, Name, ReflectedType,
223                                 ToString(), MemberTypes.Field);
224                 }
225
226                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
227                 public override extern object GetRawConstantValue ();
228
229                 public override IList<CustomAttributeData> GetCustomAttributesData () {
230                         return CustomAttributeData.GetCustomAttributes (this);
231                 }
232
233                 void CheckGeneric () {
234                         if (DeclaringType.ContainsGenericParameters)
235                                 throw new InvalidOperationException ("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true.");
236             }
237         }
238 }