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)
10 // (c) 2001-2003 Ximian, Inc.
11 // Copyright (C) 2003-2005 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
33 using System.Globalization;
34 using System.Reflection;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.Serialization;
37 using System.Security;
42 internal class MonoType : Type, ISerializable
45 [MethodImplAttribute(MethodImplOptions.InternalCall)]
46 private static extern void type_from_obj (MonoType type, Object obj);
48 internal MonoType (Object obj)
50 // this should not be used - lupus
51 type_from_obj (this, obj);
53 throw new NotImplementedException ();
56 [MethodImplAttribute(MethodImplOptions.InternalCall)]
57 private static extern TypeAttributes get_attributes (Type type);
59 protected override TypeAttributes GetAttributeFlagsImpl ()
61 return get_attributes (this);
64 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
66 CallingConventions callConvention,
68 ParameterModifier[] modifiers)
70 if (bindingAttr == BindingFlags.Default)
71 bindingAttr = BindingFlags.Public | BindingFlags.Instance;
73 ConstructorInfo[] methods = GetConstructors (bindingAttr);
74 ConstructorInfo found = null;
77 foreach (ConstructorInfo m in methods) {
78 // Under MS.NET, Standard|HasThis matches Standard...
79 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
88 throw new AmbiguousMatchException ();
89 return (ConstructorInfo) CheckMethodSecurity (found);
91 match = new MethodBase [count];
96 foreach (ConstructorInfo m in methods) {
97 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
103 binder = Binder.DefaultBinder;
104 return (ConstructorInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
107 [MethodImplAttribute(MethodImplOptions.InternalCall)]
108 internal extern ConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, Type reflected_type);
110 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
112 return GetConstructors_internal (bindingAttr, this);
115 [MethodImplAttribute(MethodImplOptions.InternalCall)]
116 extern EventInfo InternalGetEvent (string name, BindingFlags bindingAttr);
118 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
121 throw new ArgumentNullException ("name");
123 return InternalGetEvent (name, bindingAttr);
126 [MethodImplAttribute(MethodImplOptions.InternalCall)]
127 internal extern EventInfo[] GetEvents_internal (BindingFlags bindingAttr, Type reflected_type);
129 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
131 return GetEvents_internal (bindingAttr, this);
134 [MethodImplAttribute(MethodImplOptions.InternalCall)]
135 public extern override FieldInfo GetField (string name, BindingFlags bindingAttr);
137 [MethodImplAttribute(MethodImplOptions.InternalCall)]
138 internal extern FieldInfo[] GetFields_internal (BindingFlags bindingAttr, Type reflected_type);
140 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
142 return GetFields_internal (bindingAttr, this);
145 public override Type GetInterface (string name, bool ignoreCase)
148 throw new ArgumentNullException ();
150 Type[] interfaces = GetInterfaces();
152 foreach (Type type in interfaces) {
153 if (String.Compare (type.Name, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
155 if (String.Compare (type.FullName, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
162 [MethodImplAttribute(MethodImplOptions.InternalCall)]
163 public extern override Type[] GetInterfaces();
165 public override MemberInfo[] GetMembers( BindingFlags bindingAttr)
167 return FindMembers (MemberTypes.All, bindingAttr, null, null);
170 [MethodImplAttribute(MethodImplOptions.InternalCall)]
171 internal extern MethodInfo [] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type);
173 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
175 return GetMethodsByName (null, bindingAttr, false, this);
178 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
180 CallingConventions callConvention,
181 Type[] types, ParameterModifier[] modifiers)
183 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
184 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
185 MethodInfo found = null;
189 foreach (MethodInfo m in methods) {
190 // Under MS.NET, Standard|HasThis matches Standard...
191 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
200 if (count == 1 && types == null)
201 return (MethodInfo) CheckMethodSecurity (found);
203 match = new MethodBase [count];
208 foreach (MethodInfo m in methods) {
209 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
216 return (MethodInfo) CheckMethodSecurity (Binder.FindMostDerivedMatch (match));
219 binder = Binder.DefaultBinder;
221 return (MethodInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
224 [MethodImplAttribute(MethodImplOptions.InternalCall)]
225 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
227 [MethodImplAttribute(MethodImplOptions.InternalCall)]
228 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
230 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
232 return GetCorrespondingInflatedMethod (fromNoninstanciated);
235 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
237 return GetCorrespondingInflatedConstructor (fromNoninstanciated);
240 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
242 /* create sensible flags from given FieldInfo */
243 BindingFlags flags = fromNoninstanciated.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
244 flags |= fromNoninstanciated.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
245 return GetField (fromNoninstanciated.Name, flags);
248 [MethodImplAttribute(MethodImplOptions.InternalCall)]
249 public extern override Type GetNestedType (string name, BindingFlags bindingAttr);
251 [MethodImplAttribute(MethodImplOptions.InternalCall)]
252 public extern override Type[] GetNestedTypes (BindingFlags bindingAttr);
254 [MethodImplAttribute(MethodImplOptions.InternalCall)]
255 internal extern PropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, bool icase, Type reflected_type);
257 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
259 return GetPropertiesByName (null, bindingAttr, false, this);
262 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
263 Binder binder, Type returnType,
265 ParameterModifier[] modifiers)
267 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
268 PropertyInfo [] props = GetPropertiesByName (name, bindingAttr, ignoreCase, this);
269 int count = props.Length;
273 if (count == 1 && (types == null || types.Length == 0))
277 binder = Binder.DefaultBinder;
279 return binder.SelectProperty (bindingAttr, props, returnType, types, modifiers);
282 protected override bool HasElementTypeImpl ()
284 return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
287 protected override bool IsArrayImpl ()
289 return Type.IsArrayImpl (this);
292 [MethodImplAttribute(MethodImplOptions.InternalCall)]
293 protected extern override bool IsByRefImpl ();
295 [MethodImplAttribute (MethodImplOptions.InternalCall)]
296 protected extern override bool IsCOMObjectImpl ();
298 [MethodImplAttribute(MethodImplOptions.InternalCall)]
299 protected extern override bool IsPointerImpl ();
301 [MethodImplAttribute(MethodImplOptions.InternalCall)]
302 protected extern override bool IsPrimitiveImpl ();
304 public override bool IsSubclassOf (Type type)
307 throw new ArgumentNullException ("type");
309 return base.IsSubclassOf (type);
312 public override object InvokeMember (string name, BindingFlags invokeAttr,
313 Binder binder, object target, object[] args,
314 ParameterModifier[] modifiers,
315 CultureInfo culture, string[] namedParameters)
318 if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
319 if ((invokeAttr & (BindingFlags.GetField |
320 BindingFlags.GetField | BindingFlags.GetProperty |
321 BindingFlags.SetProperty)) != 0)
322 throw new ArgumentException ("invokeAttr");
323 } else if (name == null)
324 throw new ArgumentNullException ("name");
325 if ((invokeAttr & BindingFlags.GetField) != 0 && (invokeAttr & BindingFlags.SetField) != 0)
326 throw new ArgumentException ("invokeAttr");
327 if ((invokeAttr & BindingFlags.GetProperty) != 0 && (invokeAttr & BindingFlags.SetProperty) != 0)
328 throw new ArgumentException ("invokeAttr");
329 if ((invokeAttr & BindingFlags.InvokeMethod) != 0 && (invokeAttr & (BindingFlags.SetProperty|BindingFlags.SetField)) != 0)
330 throw new ArgumentException ("invokeAttr");
331 if ((namedParameters != null) && ((args == null) || args.Length < namedParameters.Length))
332 throw new ArgumentException ("namedParameters cannot be more than named arguments in number");
334 /* set some defaults if none are provided :-( */
335 if ((invokeAttr & (BindingFlags.Public|BindingFlags.NonPublic)) == 0)
336 invokeAttr |= BindingFlags.Public;
337 if ((invokeAttr & (BindingFlags.Static|BindingFlags.Instance)) == 0)
338 invokeAttr |= BindingFlags.Static|BindingFlags.Instance;
341 binder = Binder.DefaultBinder;
342 if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
343 /* the name is ignored */
344 invokeAttr |= BindingFlags.DeclaredOnly;
345 ConstructorInfo[] ctors = GetConstructors (invokeAttr);
347 MethodBase ctor = binder.BindToMethod (invokeAttr, ctors, ref args, modifiers, culture, namedParameters, out state);
349 if (this.IsValueType && args == null)
350 return Activator.CreateInstanceInternal (this);
352 throw new MissingMethodException ("Constructor on type '" + FullName + "' not found.");
354 object result = ctor.Invoke (target, invokeAttr, binder, args, culture);
355 binder.ReorderArgumentArray (ref args, state);
358 if (name == String.Empty && Attribute.IsDefined (this, typeof (DefaultMemberAttribute))) {
359 DefaultMemberAttribute attr = (DefaultMemberAttribute) Attribute.GetCustomAttribute (this, typeof (DefaultMemberAttribute));
360 name = attr.MemberName;
362 bool ignoreCase = (invokeAttr & BindingFlags.IgnoreCase) != 0;
363 string throwMissingMethodDescription = null;
364 bool throwMissingFieldException = false;
366 if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
367 MethodInfo[] methods = GetMethodsByName (name, invokeAttr, ignoreCase, this);
369 MethodBase m = binder.BindToMethod (invokeAttr, methods, ref args, modifiers, culture, namedParameters, out state);
371 if (methods.Length > 0)
372 throwMissingMethodDescription = "The best match for method " + name + " has some invalid parameter.";
374 throwMissingMethodDescription = "Cannot find method " + name + ".";
376 object result = m.Invoke (target, invokeAttr, binder, args, culture);
377 binder.ReorderArgumentArray (ref args, state);
381 if ((invokeAttr & BindingFlags.GetField) != 0) {
382 FieldInfo f = GetField (name, invokeAttr);
384 return f.GetValue (target);
385 } else if ((invokeAttr & BindingFlags.GetProperty) == 0) {
386 throwMissingFieldException = true;
388 /* try GetProperty */
389 } else if ((invokeAttr & BindingFlags.SetField) != 0) {
390 if ((args == null) || args.Length != 1)
391 throw new ArgumentException ("invokeAttr");
393 FieldInfo f = GetField (name, invokeAttr);
395 f.SetValue (target, args [0]);
397 } else if ((invokeAttr & BindingFlags.SetProperty) == 0) {
398 throwMissingFieldException = true;
400 /* try SetProperty */
402 if ((invokeAttr & BindingFlags.GetProperty) != 0) {
403 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
406 for (i = 0; i < properties.Length; ++i) {
407 if ((properties [i].GetGetMethod (true) != null))
410 MethodBase[] smethods = new MethodBase [count];
412 for (i = 0; i < properties.Length; ++i) {
413 MethodBase mb = properties [i].GetGetMethod (true);
415 smethods [count++] = mb;
417 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
419 throwMissingFieldException = true;
421 object result = m.Invoke (target, invokeAttr, binder, args, culture);
422 binder.ReorderArgumentArray (ref args, state);
425 } else if ((invokeAttr & BindingFlags.SetProperty) != 0) {
426 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
429 for (i = 0; i < properties.Length; ++i) {
430 if (properties [i].GetSetMethod (true) != null)
433 MethodBase[] smethods = new MethodBase [count];
435 for (i = 0; i < properties.Length; ++i) {
436 MethodBase mb = properties [i].GetSetMethod (true);
438 smethods [count++] = mb;
440 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
442 throwMissingFieldException = true;
444 object result = m.Invoke (target, invokeAttr, binder, args, culture);
445 binder.ReorderArgumentArray (ref args, state);
449 if (throwMissingMethodDescription != null)
450 throw new MissingMethodException(throwMissingMethodDescription);
451 if (throwMissingFieldException)
452 throw new MissingFieldException("Cannot find variable " + name + ".");
457 [MethodImplAttribute(MethodImplOptions.InternalCall)]
458 public extern override Type GetElementType ();
460 public override Type UnderlyingSystemType {
462 // This has _nothing_ to do with getting the base type of an enum etc.
467 public extern override Assembly Assembly {
468 [MethodImplAttribute(MethodImplOptions.InternalCall)]
472 public override string AssemblyQualifiedName {
474 return getFullName (true, true);
478 [MethodImplAttribute(MethodImplOptions.InternalCall)]
479 private extern string getFullName(bool full_name, bool assembly_qualified);
481 public extern override Type BaseType {
482 [MethodImplAttribute(MethodImplOptions.InternalCall)]
486 public override string FullName {
488 return getFullName (true, false);
492 public override Guid GUID {
494 object[] att = GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), true);
497 return new Guid(((System.Runtime.InteropServices.GuidAttribute)att[0]).Value);
501 public override bool IsDefined (Type attributeType, bool inherit)
503 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
506 public override object[] GetCustomAttributes (bool inherit)
508 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
511 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
513 if (attributeType == null)
515 throw new ArgumentNullException("attributeType");
518 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
521 public override MemberTypes MemberType {
523 if (DeclaringType != null)
524 return MemberTypes.NestedType;
526 return MemberTypes.TypeInfo;
530 public extern override string Name {
531 [MethodImplAttribute(MethodImplOptions.InternalCall)]
535 public extern override string Namespace {
536 [MethodImplAttribute(MethodImplOptions.InternalCall)]
540 public extern override Module Module {
541 [MethodImplAttribute(MethodImplOptions.InternalCall)]
545 public extern override Type DeclaringType {
546 [MethodImplAttribute(MethodImplOptions.InternalCall)]
550 public override Type ReflectedType {
552 return DeclaringType;
556 public override RuntimeTypeHandle TypeHandle {
562 [MethodImplAttribute(MethodImplOptions.InternalCall)]
563 public extern override int GetArrayRank ();
565 public void GetObjectData(SerializationInfo info, StreamingContext context)
567 UnitySerializationHolder.GetTypeData (this, info, context);
570 public override string ToString()
572 return getFullName (false, false);
575 #if NET_2_0 || BOOTSTRAP_NET_2_0
576 [MethodImplAttribute(MethodImplOptions.InternalCall)]
577 public extern override Type [] GetGenericArguments ();
579 public override bool ContainsGenericParameters {
581 if (IsGenericParameter)
585 foreach (Type arg in GetGenericArguments ())
586 if (arg.ContainsGenericParameters)
591 return GetElementType ().ContainsGenericParameters;
597 public extern override bool IsGenericParameter {
598 [MethodImplAttribute(MethodImplOptions.InternalCall)]
602 public extern override MethodBase DeclaringMethod {
603 [MethodImplAttribute(MethodImplOptions.InternalCall)]
608 private MethodBase CheckMethodSecurity (MethodBase mb)
610 if (!SecurityManager.SecurityEnabled || (mb == null))
613 // Sadly we have no way to know which kind of security action this is
614 // so we must do it the hard way. Actually this isn't so bad
615 // because we can skip the (mb.Attributes & MethodAttributes.HasSecurity)
616 // icall required (and do it ourselves)
618 // this (unlike the Invoke step) is _and stays_ a LinkDemand (caller)
619 return SecurityManager.ReflectedLinkDemandQuery (mb) ? mb : null;