2005-03-13 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / class / corlib / System / MonoType.cs
1 //
2 // System.MonoType
3 //
4 // Authors: 
5 //      Sean MacIsaac (macisaac@ximian.com)
6 //      Paolo Molaro (lupus@ximian.com)
7 //      Patrik Torstensson (patrik.torstensson@labs2.com)
8 //      Gonzalo Paniagua (gonzalo@ximian.com)
9 //
10 // (c) 2001-2003 Ximian, Inc.
11 // Copyright (C) 2003-2005 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Globalization;
34 using System.Reflection;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.Serialization;
37 using System.Security;
38
39 namespace System
40 {
41         [Serializable]
42         internal class MonoType : Type, ISerializable
43         {
44
45                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
46                 private static extern void type_from_obj (MonoType type, Object obj);
47                 
48                 [MonoTODO]
49                 internal MonoType (Object obj)
50                 {
51                         // this should not be used - lupus
52                         type_from_obj (this, obj);
53                         
54                         throw new NotImplementedException ();
55                 }
56
57                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
58                 private static extern TypeAttributes get_attributes (Type type);
59         
60                 protected override TypeAttributes GetAttributeFlagsImpl ()
61                 {
62                         return get_attributes (this);
63                 }
64
65                 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
66                                                                        Binder binder,
67                                                                        CallingConventions callConvention,
68                                                                        Type[] types,
69                                                                        ParameterModifier[] modifiers)
70                 {
71                         if (bindingAttr == BindingFlags.Default)
72                                 bindingAttr = BindingFlags.Public | BindingFlags.Instance;
73
74                         ConstructorInfo[] methods = GetConstructors (bindingAttr);
75                         ConstructorInfo found = null;
76                         MethodBase[] match;
77                         int count = 0;
78                         foreach (ConstructorInfo m in methods) {
79                                 // Under MS.NET, Standard|HasThis matches Standard...
80                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
81                                         continue;
82                                 found = m;
83                                 count++;
84                         }
85                         if (count == 0)
86                                 return null;
87                         if (types == null) {
88                                 if (count > 1)
89                                         throw new AmbiguousMatchException ();
90                                 return (ConstructorInfo) CheckMethodSecurity (found);
91                         }
92                         match = new MethodBase [count];
93                         if (count == 1)
94                                 match [0] = found;
95                         else {
96                                 count = 0;
97                                 foreach (ConstructorInfo m in methods) {
98                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
99                                                 continue;
100                                         match [count++] = m;
101                                 }
102                         }
103                         if (binder == null)
104                                 binder = Binder.DefaultBinder;
105                         return (ConstructorInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
106                 }
107
108                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
109                 internal extern ConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, Type reflected_type);
110
111                 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
112                 {
113                         return GetConstructors_internal (bindingAttr, this);
114                 }
115
116                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
117                 extern EventInfo InternalGetEvent (string name, BindingFlags bindingAttr);
118
119                 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
120                 {
121                         if (name == null)
122                                 throw new ArgumentNullException ("name");
123
124                         return InternalGetEvent (name, bindingAttr);
125                 }
126
127                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
128                 internal extern EventInfo[] GetEvents_internal (BindingFlags bindingAttr, Type reflected_type);
129
130                 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
131                 {
132                         return GetEvents_internal (bindingAttr, this);
133                 }
134
135                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
136                 public extern override FieldInfo GetField (string name, BindingFlags bindingAttr);
137
138                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
139                 internal extern FieldInfo[] GetFields_internal (BindingFlags bindingAttr, Type reflected_type);
140
141                 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
142                 {
143                         return GetFields_internal (bindingAttr, this);
144                 }
145                 
146                 public override Type GetInterface (string name, bool ignoreCase)
147                 {
148                         if (name == null)
149                                 throw new ArgumentNullException ();
150
151                         Type[] interfaces = GetInterfaces();
152
153                         foreach (Type type in interfaces) {
154                                 if (String.Compare (type.Name, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
155                                         return type;
156                                 if (String.Compare (type.FullName, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
157                                         return type;
158                         }
159
160                         return null;
161                 }
162
163                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
164                 public extern override Type[] GetInterfaces();
165                 
166                 public override MemberInfo[] GetMembers( BindingFlags bindingAttr)
167                 {
168                         return FindMembers (MemberTypes.All, bindingAttr, null, null);
169                 }
170
171                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
172                 internal extern MethodInfo [] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type);
173
174                 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
175                 {
176                         return GetMethodsByName (null, bindingAttr, false, this);
177                 }
178
179                 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
180                                                              Binder binder,
181                                                              CallingConventions callConvention,
182                                                              Type[] types, ParameterModifier[] modifiers)
183                 {
184                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
185                         MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
186                         MethodInfo found = null;
187                         MethodBase[] match;
188                         int typesLen = (types != null) ? types.Length : 0;
189                         int count = 0;
190                         
191                         foreach (MethodInfo m in methods) {
192                                 // Under MS.NET, Standard|HasThis matches Standard...
193                                 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
194                                         continue;
195                                 found = m;
196                                 count++;
197                         }
198
199                         if (count == 0)
200                                 return null;
201                         
202                         if (count == 1 && typesLen == 0) 
203                                 return (MethodInfo) CheckMethodSecurity (found);
204
205                         match = new MethodBase [count];
206                         if (count == 1)
207                                 match [0] = found;
208                         else {
209                                 count = 0;
210                                 foreach (MethodInfo m in methods) {
211                                         if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
212                                                 continue;
213                                         match [count++] = m;
214                                 }
215                         }
216                         
217                         if (types == null) 
218                                 return (MethodInfo) CheckMethodSecurity (Binder.FindMostDerivedMatch (match));
219
220                         if (binder == null)
221                                 binder = Binder.DefaultBinder;
222                         
223                         return (MethodInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
224                 }
225
226                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
227                 public extern override Type GetNestedType (string name, BindingFlags bindingAttr);
228
229                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
230                 public extern override Type[] GetNestedTypes (BindingFlags bindingAttr);
231
232                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
233                 internal extern PropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, bool icase, Type reflected_type);
234
235                 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
236                 {
237                         return GetPropertiesByName (null, bindingAttr, false, this);
238                 }
239
240                 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
241                                                                  Binder binder, Type returnType,
242                                                                  Type[] types,
243                                                                  ParameterModifier[] modifiers)
244                 {
245                         bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
246                         PropertyInfo [] props = GetPropertiesByName (name, bindingAttr, ignoreCase, this);
247                         int count = props.Length;
248                         if (count == 0)
249                                 return null;
250                         
251                         if (count == 1 && (types == null || types.Length == 0)) 
252                                 return props [0];
253
254                         if (binder == null)
255                                 binder = Binder.DefaultBinder;
256                         
257                         return binder.SelectProperty (bindingAttr, props, returnType, types, modifiers);
258                 }
259
260                 protected override bool HasElementTypeImpl ()
261                 {
262                         return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
263                 }
264
265                 protected override bool IsArrayImpl ()
266                 {
267                         return Type.IsArrayImpl (this);
268                 }
269
270                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
271                 protected extern override bool IsByRefImpl ();
272
273                 protected override bool IsCOMObjectImpl ()
274                 {
275                         return false;
276                 }
277
278                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
279                 protected extern override bool IsPointerImpl ();
280
281                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
282                 protected extern override bool IsPrimitiveImpl ();
283
284                 protected override bool IsValueTypeImpl ()
285                 {
286                         return type_is_subtype_of (this, typeof (System.ValueType), false) &&
287                                 this != typeof (System.ValueType) &&
288                                 this != typeof (System.Enum);
289                 }
290                 
291                 public override object InvokeMember (string name, BindingFlags invokeAttr,
292                                                      Binder binder, object target, object[] args,
293                                                      ParameterModifier[] modifiers,
294                                                      CultureInfo culture, string[] namedParameters)
295                 {
296
297                         if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
298                                 if ((invokeAttr & (BindingFlags.GetField |
299                                                 BindingFlags.GetField | BindingFlags.GetProperty |
300                                                 BindingFlags.SetProperty)) != 0)
301                                         throw new ArgumentException ("invokeAttr");
302                         } else if (name == null)
303                                 throw new ArgumentNullException ("name");
304                         if ((invokeAttr & BindingFlags.GetField) != 0 && (invokeAttr & BindingFlags.SetField) != 0)
305                                 throw new ArgumentException ("invokeAttr");
306                         if ((invokeAttr & BindingFlags.GetProperty) != 0 && (invokeAttr & BindingFlags.SetProperty) != 0)
307                                 throw new ArgumentException ("invokeAttr");
308                         if ((invokeAttr & BindingFlags.InvokeMethod) != 0 && (invokeAttr & (BindingFlags.SetProperty|BindingFlags.SetField)) != 0)
309                                 throw new ArgumentException ("invokeAttr");
310                         if ((invokeAttr & BindingFlags.SetField) != 0 && ((args == null) || args.Length != 1))
311                                 throw new ArgumentException ("invokeAttr");
312                         if ((namedParameters != null) && ((args == null) || args.Length < namedParameters.Length))
313                                 throw new ArgumentException ("namedParameters cannot be more than named arguments in number");
314
315                         /* set some defaults if none are provided :-( */
316                         if ((invokeAttr & (BindingFlags.Public|BindingFlags.NonPublic)) == 0)
317                                 invokeAttr |= BindingFlags.Public;
318                         if ((invokeAttr & (BindingFlags.Static|BindingFlags.Instance)) == 0)
319                                 invokeAttr |= BindingFlags.Static|BindingFlags.Instance;
320
321                         if (binder == null)
322                                 binder = Binder.DefaultBinder;
323                         if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
324                                 /* the name is ignored */
325                                 invokeAttr |= BindingFlags.DeclaredOnly;
326                                 ConstructorInfo[] ctors = GetConstructors (invokeAttr);
327                                 object state = null;
328                                 MethodBase ctor = binder.BindToMethod (invokeAttr, ctors, ref args, modifiers, culture, namedParameters, out state);
329                                 if (ctor == null) {
330                                         if (this.IsValueType && args == null)
331                                                 return Activator.CreateInstanceInternal (this);
332                                         
333                                         throw new MissingMethodException ("Constructor on type '" + FullName + "' not found.");
334                                 }
335                                 object result = ctor.Invoke (target, invokeAttr, binder, args, culture);
336                                 binder.ReorderArgumentArray (ref args, state);
337                                 return result;
338                         }
339                         if (name == String.Empty && Attribute.IsDefined (this, typeof (DefaultMemberAttribute))) {
340                                 DefaultMemberAttribute attr = (DefaultMemberAttribute) Attribute.GetCustomAttribute (this, typeof (DefaultMemberAttribute));
341                                 name = attr.MemberName;
342                         }
343                         bool ignoreCase = (invokeAttr & BindingFlags.IgnoreCase) != 0;
344                         bool throwMissingMethodException = false;
345                         bool throwMissingFieldException = false;
346                         if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
347                                 MethodInfo[] methods = GetMethodsByName (name, invokeAttr, ignoreCase, this);
348                                 object state = null;
349                                 MethodBase m = binder.BindToMethod (invokeAttr, methods, ref args, modifiers, culture, namedParameters, out state);
350                                 if (m == null) {
351                                         throwMissingMethodException = true;
352                                 } else {
353                                         object result = m.Invoke (target, invokeAttr, binder, args, culture);
354                                         binder.ReorderArgumentArray (ref args, state);
355                                         return result;
356                                 }
357                         }
358                         if ((invokeAttr & BindingFlags.GetField) != 0) {
359                                 FieldInfo f = GetField (name, invokeAttr);
360                                 if (f != null) {
361                                         return f.GetValue (target);
362                                 } else if ((invokeAttr & BindingFlags.GetProperty) == 0) {
363                                         throwMissingFieldException = true;
364                                 }
365                                 /* try GetProperty */
366                         } else if ((invokeAttr & BindingFlags.SetField) != 0) {
367                                 FieldInfo f = GetField (name, invokeAttr);
368                                 if (f != null) {
369                                         f.SetValue (target, args [0]);
370                                         return null;
371                                 } else if ((invokeAttr & BindingFlags.SetProperty) == 0) {
372                                         throwMissingFieldException = true;
373                                 }
374                                 /* try SetProperty */
375                         }
376                         if ((invokeAttr & BindingFlags.GetProperty) != 0) {
377                                 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
378                                 object state = null;
379                                 int i, count = 0;
380                                 for (i = 0; i < properties.Length; ++i) {
381                                         if ((properties [i].GetGetMethod (true) != null))
382                                                 count++;
383                                 }
384                                 MethodBase[] smethods = new MethodBase [count];
385                                 count = 0;
386                                 for (i = 0; i < properties.Length; ++i) {
387                                         MethodBase mb = properties [i].GetGetMethod (true);
388                                         if (mb != null)
389                                                 smethods [count++] = mb;
390                                 }
391                                 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
392                                 if (m == null) {
393                                         throwMissingFieldException = true;
394                                 } else {
395                                         object result = m.Invoke (target, invokeAttr, binder, args, culture);
396                                         binder.ReorderArgumentArray (ref args, state);
397                                         return result;
398                                 }
399                         } else if ((invokeAttr & BindingFlags.SetProperty) != 0) {
400                                 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
401                                 object state = null;
402                                 int i, count = 0;
403                                 for (i = 0; i < properties.Length; ++i) {
404                                         if (properties [i].GetSetMethod (true) != null)
405                                                 count++;
406                                 }
407                                 MethodBase[] smethods = new MethodBase [count];
408                                 count = 0;
409                                 for (i = 0; i < properties.Length; ++i) {
410                                         MethodBase mb = properties [i].GetSetMethod (true);
411                                         if (mb != null)
412                                                 smethods [count++] = mb;
413                                 }
414                                 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
415                                 if (m == null) {
416                                         throwMissingFieldException = true;
417                                 } else {
418                                         object result = m.Invoke (target, invokeAttr, binder, args, culture);
419                                         binder.ReorderArgumentArray (ref args, state);
420                                         return result;
421                                 }
422                         }
423                         if (throwMissingMethodException)
424                                 throw new MissingMethodException();
425                         if (throwMissingFieldException)
426                                 throw new MissingFieldException();
427
428                         return null;
429                 }
430
431                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
432                 public extern override Type GetElementType ();
433
434                 public override Type UnderlyingSystemType {
435                         get {
436                                 // This has _nothing_ to do with getting the base type of an enum etc.
437                                 return this;
438                         }
439                 }
440
441                 public extern override Assembly Assembly {
442                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
443                         get;
444                 }
445
446                 public override string AssemblyQualifiedName {
447                         get {
448                                 return getFullName (false) + ", " + Assembly.GetName ().ToString ();
449                         }
450                 }
451
452                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
453                 private extern string getFullName(bool full_name);
454
455                 public extern override Type BaseType {
456                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
457                         get;
458                 }
459
460                 public override string FullName {
461                         get {
462                                 return getFullName (false);
463                         }
464                 }
465
466                 public override Guid GUID {
467                         get {
468                                 return Guid.Empty;
469                         }
470                 }
471
472                 public override bool IsDefined (Type attributeType, bool inherit)
473                 {
474                         return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
475                 }
476
477                 public override object[] GetCustomAttributes (bool inherit)
478                 {
479                         return MonoCustomAttrs.GetCustomAttributes (this, inherit);
480                 }
481
482                 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
483                 {
484                         if (attributeType == null)
485                         {
486                                 throw new ArgumentNullException("attributeType");
487                         }
488                         
489                         return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
490                 }
491
492                 public override MemberTypes MemberType {
493                         get {
494                                 if (DeclaringType != null)
495                                         return MemberTypes.NestedType;
496                                 else
497                                         return MemberTypes.TypeInfo;
498                         }
499                 }
500
501                 public extern override string Name {
502                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
503                         get;
504                 }
505
506                 public extern override string Namespace {
507                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
508                         get;
509                 }
510
511                 public extern override Module Module {
512                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
513                         get;
514                 }
515
516                 public extern override Type DeclaringType {
517                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
518                         get;
519                 }
520
521                 public override Type ReflectedType {
522                         get {
523                                 return DeclaringType;
524                         }
525                 }
526
527                 public override RuntimeTypeHandle TypeHandle {
528                         get {
529                                 return _impl;
530                         }
531                 }
532
533                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
534                 public extern override int GetArrayRank ();
535
536                 public void GetObjectData(SerializationInfo info, StreamingContext context)
537                 {
538                         UnitySerializationHolder.GetTypeData (this, info, context);
539                 }
540
541                 public override string ToString()
542                 {
543                         return getFullName (true);
544                 }
545
546 #if NET_2_0 || BOOTSTRAP_NET_2_0
547                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
548                 public extern override Type [] GetGenericArguments ();
549
550                 public extern override bool HasGenericArguments {
551                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
552                         get;
553                 }
554
555                 public override bool ContainsGenericParameters {
556                         get {
557                                 if (IsGenericParameter)
558                                         return true;
559
560                                 if (HasGenericArguments) {
561                                         foreach (Type arg in GetGenericArguments ())
562                                                 if (arg.ContainsGenericParameters)
563                                                         return true;
564                                 }
565
566                                 if (HasElementType)
567                                         return GetElementType ().ContainsGenericParameters;
568
569                                 return false;
570                         }
571                 }
572
573                 public extern override bool IsGenericParameter {
574                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
575                         get;
576                 }
577
578                 public extern override MethodInfo DeclaringMethod {
579                         [MethodImplAttribute(MethodImplOptions.InternalCall)]
580                         get;
581                 }
582 #endif
583
584                 private MethodBase CheckMethodSecurity (MethodBase mb)
585                 {
586                         if (!SecurityManager.SecurityEnabled || (mb == null))
587                                 return mb;
588
589                         // Sadly we have no way to know which kind of security action this is
590                         // so we must do it the hard way. Actually this isn't so bad 
591                         // because we can skip the (mb.Attributes & MethodAttributes.HasSecurity)
592                         // icall required (and do it ourselves)
593
594                         // this (unlike the Invoke step) is _and stays_ a LinkDemand (caller)
595                         return SecurityManager.ReflectedLinkDemandQuery (mb) ? mb : null;
596                 }
597         }
598 }