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.Collections.Generic;
34 using System.Globalization;
35 using System.Reflection;
36 using System.Runtime.CompilerServices;
37 using System.Runtime.Serialization;
38 using System.Security;
42 // Contains information about the type which is expensive to compute
43 internal class MonoTypeInfo {
44 public string full_name;
45 public ConstructorInfo default_ctor;
49 internal class MonoType : Type, ISerializable
52 MonoTypeInfo type_info;
54 [MethodImplAttribute(MethodImplOptions.InternalCall)]
55 private static extern void type_from_obj (MonoType type, Object obj);
57 internal MonoType (Object obj)
59 // this should not be used - lupus
60 type_from_obj (this, obj);
62 throw new NotImplementedException ();
65 [MethodImplAttribute(MethodImplOptions.InternalCall)]
66 private static extern TypeAttributes get_attributes (Type type);
68 internal ConstructorInfo GetDefaultConstructor () {
69 ConstructorInfo ctor = null;
71 if (type_info == null)
72 type_info = new MonoTypeInfo ();
73 if ((ctor = type_info.default_ctor) == null) {
74 const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic;
76 ctor = type_info.default_ctor = GetConstructor (flags, null, CallingConventions.Any, Type.EmptyTypes, null);
82 protected override TypeAttributes GetAttributeFlagsImpl ()
84 return get_attributes (this);
87 protected override ConstructorInfo GetConstructorImpl (BindingFlags bindingAttr,
89 CallingConventions callConvention,
91 ParameterModifier[] modifiers)
93 ConstructorInfo[] methods = GetConstructors (bindingAttr);
94 return GetConstructorImpl (methods, bindingAttr, binder, callConvention, types, modifiers);
97 internal static ConstructorInfo GetConstructorImpl (ConstructorInfo[] methods, BindingFlags bindingAttr,
99 CallingConventions callConvention,
101 ParameterModifier[] modifiers)
103 if (bindingAttr == BindingFlags.Default)
104 bindingAttr = BindingFlags.Public | BindingFlags.Instance;
106 ConstructorInfo found = null;
109 foreach (ConstructorInfo m in methods) {
110 // Under MS.NET, Standard|HasThis matches Standard...
111 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
120 throw new AmbiguousMatchException ();
121 return (ConstructorInfo) CheckMethodSecurity (found);
123 match = new MethodBase [count];
128 foreach (ConstructorInfo m in methods) {
129 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
135 binder = Binder.DefaultBinder;
136 return (ConstructorInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
139 [MethodImplAttribute(MethodImplOptions.InternalCall)]
140 internal extern ConstructorInfo[] GetConstructors_internal (BindingFlags bindingAttr, Type reflected_type);
142 public override ConstructorInfo[] GetConstructors (BindingFlags bindingAttr)
144 return GetConstructors_internal (bindingAttr, this);
147 [MethodImplAttribute(MethodImplOptions.InternalCall)]
148 extern EventInfo InternalGetEvent (string name, BindingFlags bindingAttr);
150 public override EventInfo GetEvent (string name, BindingFlags bindingAttr)
153 throw new ArgumentNullException ("name");
155 return InternalGetEvent (name, bindingAttr);
158 [MethodImplAttribute(MethodImplOptions.InternalCall)]
159 internal extern EventInfo[] GetEvents_internal (BindingFlags bindingAttr, Type reflected_type);
161 public override EventInfo[] GetEvents (BindingFlags bindingAttr)
163 return GetEvents_internal (bindingAttr, this);
166 [MethodImplAttribute(MethodImplOptions.InternalCall)]
167 public extern override FieldInfo GetField (string name, BindingFlags bindingAttr);
169 [MethodImplAttribute(MethodImplOptions.InternalCall)]
170 internal extern FieldInfo[] GetFields_internal (BindingFlags bindingAttr, Type reflected_type);
172 public override FieldInfo[] GetFields (BindingFlags bindingAttr)
174 return GetFields_internal (bindingAttr, this);
177 public override Type GetInterface (string name, bool ignoreCase)
180 throw new ArgumentNullException ();
182 Type[] interfaces = GetInterfaces();
184 foreach (Type type in interfaces) {
185 /*We must compare against the generic type definition*/
186 Type t = type.IsGenericType ? type.GetGenericTypeDefinition () : type;
188 if (String.Compare (t.Name, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
190 if (String.Compare (t.FullName, name, ignoreCase, CultureInfo.InvariantCulture) == 0)
197 [MethodImplAttribute(MethodImplOptions.InternalCall)]
198 public extern override Type[] GetInterfaces();
200 public override MemberInfo[] GetMembers( BindingFlags bindingAttr)
202 return FindMembers (MemberTypes.All, bindingAttr, null, null);
205 [MethodImplAttribute(MethodImplOptions.InternalCall)]
206 internal extern MethodInfo [] GetMethodsByName (string name, BindingFlags bindingAttr, bool ignoreCase, Type reflected_type);
208 public override MethodInfo [] GetMethods (BindingFlags bindingAttr)
210 return GetMethodsByName (null, bindingAttr, false, this);
213 protected override MethodInfo GetMethodImpl (string name, BindingFlags bindingAttr,
215 CallingConventions callConvention,
216 Type[] types, ParameterModifier[] modifiers)
218 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
219 MethodInfo[] methods = GetMethodsByName (name, bindingAttr, ignoreCase, this);
220 MethodInfo found = null;
224 foreach (MethodInfo m in methods) {
225 // Under MS.NET, Standard|HasThis matches Standard...
226 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
235 if (count == 1 && types == null)
236 return (MethodInfo) CheckMethodSecurity (found);
238 match = new MethodBase [count];
243 foreach (MethodInfo m in methods) {
244 if (callConvention != CallingConventions.Any && ((m.CallingConvention & callConvention) != callConvention))
251 return (MethodInfo) CheckMethodSecurity (Binder.FindMostDerivedMatch (match));
254 binder = Binder.DefaultBinder;
256 return (MethodInfo) CheckMethodSecurity (binder.SelectMethod (bindingAttr, match, types, modifiers));
259 [MethodImplAttribute(MethodImplOptions.InternalCall)]
260 extern MethodInfo GetCorrespondingInflatedMethod (MethodInfo generic);
262 [MethodImplAttribute(MethodImplOptions.InternalCall)]
263 extern ConstructorInfo GetCorrespondingInflatedConstructor (ConstructorInfo generic);
265 internal override MethodInfo GetMethod (MethodInfo fromNoninstanciated)
267 if (fromNoninstanciated == null)
268 throw new ArgumentNullException ("fromNoninstanciated");
269 return GetCorrespondingInflatedMethod (fromNoninstanciated);
272 internal override ConstructorInfo GetConstructor (ConstructorInfo fromNoninstanciated)
274 if (fromNoninstanciated == null)
275 throw new ArgumentNullException ("fromNoninstanciated");
276 return GetCorrespondingInflatedConstructor (fromNoninstanciated);
279 internal override FieldInfo GetField (FieldInfo fromNoninstanciated)
281 /* create sensible flags from given FieldInfo */
282 BindingFlags flags = fromNoninstanciated.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
283 flags |= fromNoninstanciated.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
284 return GetField (fromNoninstanciated.Name, flags);
287 [MethodImplAttribute(MethodImplOptions.InternalCall)]
288 public extern override Type GetNestedType (string name, BindingFlags bindingAttr);
290 [MethodImplAttribute(MethodImplOptions.InternalCall)]
291 public extern override Type[] GetNestedTypes (BindingFlags bindingAttr);
293 [MethodImplAttribute(MethodImplOptions.InternalCall)]
294 internal extern PropertyInfo[] GetPropertiesByName (string name, BindingFlags bindingAttr, bool icase, Type reflected_type);
296 public override PropertyInfo [] GetProperties (BindingFlags bindingAttr)
298 return GetPropertiesByName (null, bindingAttr, false, this);
301 protected override PropertyInfo GetPropertyImpl (string name, BindingFlags bindingAttr,
302 Binder binder, Type returnType,
304 ParameterModifier[] modifiers)
306 bool ignoreCase = ((bindingAttr & BindingFlags.IgnoreCase) != 0);
307 PropertyInfo [] props = GetPropertiesByName (name, bindingAttr, ignoreCase, this);
308 int count = props.Length;
312 if (count == 1 && (types == null || types.Length == 0) &&
313 (returnType == null || returnType == props[0].PropertyType))
317 binder = Binder.DefaultBinder;
319 return binder.SelectProperty (bindingAttr, props, returnType, types, modifiers);
322 protected override bool HasElementTypeImpl ()
324 return IsArrayImpl() || IsByRefImpl() || IsPointerImpl ();
327 protected override bool IsArrayImpl ()
329 return Type.IsArrayImpl (this);
332 [MethodImplAttribute(MethodImplOptions.InternalCall)]
333 protected extern override bool IsByRefImpl ();
335 [MethodImplAttribute (MethodImplOptions.InternalCall)]
336 protected extern override bool IsCOMObjectImpl ();
338 [MethodImplAttribute(MethodImplOptions.InternalCall)]
339 protected extern override bool IsPointerImpl ();
341 [MethodImplAttribute(MethodImplOptions.InternalCall)]
342 protected extern override bool IsPrimitiveImpl ();
344 public override bool IsSubclassOf (Type type)
347 throw new ArgumentNullException ("type");
349 return base.IsSubclassOf (type);
352 public override object InvokeMember (string name, BindingFlags invokeAttr,
353 Binder binder, object target, object[] args,
354 ParameterModifier[] modifiers,
355 CultureInfo culture, string[] namedParameters)
357 const string bindingflags_arg = "bindingFlags";
360 if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
361 if ((invokeAttr & (BindingFlags.GetField |
362 BindingFlags.GetField | BindingFlags.GetProperty |
363 BindingFlags.SetProperty)) != 0)
364 throw new ArgumentException (bindingflags_arg);
365 } else if (name == null)
366 throw new ArgumentNullException ("name");
367 if ((invokeAttr & BindingFlags.GetField) != 0 && (invokeAttr & BindingFlags.SetField) != 0)
368 throw new ArgumentException ("Cannot specify both Get and Set on a field.", bindingflags_arg);
369 if ((invokeAttr & BindingFlags.GetProperty) != 0 && (invokeAttr & BindingFlags.SetProperty) != 0)
370 throw new ArgumentException ("Cannot specify both Get and Set on a property.", bindingflags_arg);
371 if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
372 if ((invokeAttr & BindingFlags.SetField) != 0)
373 throw new ArgumentException ("Cannot specify Set on a field and Invoke on a method.", bindingflags_arg);
374 if ((invokeAttr & BindingFlags.SetProperty) != 0)
375 throw new ArgumentException ("Cannot specify Set on a property and Invoke on a method.", bindingflags_arg);
377 if ((namedParameters != null) && ((args == null) || args.Length < namedParameters.Length))
378 throw new ArgumentException ("namedParameters cannot be more than named arguments in number");
379 if ((invokeAttr & (BindingFlags.InvokeMethod|BindingFlags.CreateInstance|BindingFlags.GetField|BindingFlags.SetField|BindingFlags.GetProperty|BindingFlags.SetProperty)) == 0)
380 throw new ArgumentException ("Must specify binding flags describing the invoke operation required.", bindingflags_arg);
382 /* set some defaults if none are provided :-( */
383 if ((invokeAttr & (BindingFlags.Public|BindingFlags.NonPublic)) == 0)
384 invokeAttr |= BindingFlags.Public;
385 if ((invokeAttr & (BindingFlags.Static|BindingFlags.Instance)) == 0)
386 invokeAttr |= BindingFlags.Static|BindingFlags.Instance;
389 binder = Binder.DefaultBinder;
390 if ((invokeAttr & BindingFlags.CreateInstance) != 0) {
391 /* the name is ignored */
392 invokeAttr |= BindingFlags.DeclaredOnly;
393 ConstructorInfo[] ctors = GetConstructors (invokeAttr);
395 MethodBase ctor = binder.BindToMethod (invokeAttr, ctors, ref args, modifiers, culture, namedParameters, out state);
397 if (this.IsValueType && args == null)
398 return Activator.CreateInstanceInternal (this);
400 throw new MissingMethodException ("Constructor on type '" + FullName + "' not found.");
402 object result = ctor.Invoke (target, invokeAttr, binder, args, culture);
403 binder.ReorderArgumentArray (ref args, state);
406 if (name == String.Empty && Attribute.IsDefined (this, typeof (DefaultMemberAttribute))) {
407 DefaultMemberAttribute attr = (DefaultMemberAttribute) Attribute.GetCustomAttribute (this, typeof (DefaultMemberAttribute));
408 name = attr.MemberName;
410 bool ignoreCase = (invokeAttr & BindingFlags.IgnoreCase) != 0;
411 string throwMissingMethodDescription = null;
412 bool throwMissingFieldException = false;
414 if ((invokeAttr & BindingFlags.InvokeMethod) != 0) {
415 MethodInfo[] methods = GetMethodsByName (name, invokeAttr, ignoreCase, this);
418 args = new object [0];
419 MethodBase m = binder.BindToMethod (invokeAttr, methods, ref args, modifiers, culture, namedParameters, out state);
421 if (methods.Length > 0)
422 throwMissingMethodDescription = "The best match for method " + name + " has some invalid parameter.";
424 throwMissingMethodDescription = "Cannot find method " + name + ".";
426 ParameterInfo[] parameters = m.GetParameters();
427 for (int i = 0; i < parameters.Length; ++i) {
428 if (System.Reflection.Missing.Value == args [i] && (parameters [i].Attributes & ParameterAttributes.HasDefault) != ParameterAttributes.HasDefault)
429 throw new ArgumentException ("Used Missing.Value for argument without default value", "parameters");
431 object result = m.Invoke (target, invokeAttr, binder, args, culture);
432 binder.ReorderArgumentArray (ref args, state);
436 if ((invokeAttr & BindingFlags.GetField) != 0) {
437 FieldInfo f = GetField (name, invokeAttr);
439 return f.GetValue (target);
440 } else if ((invokeAttr & BindingFlags.GetProperty) == 0) {
441 throwMissingFieldException = true;
443 /* try GetProperty */
444 } else if ((invokeAttr & BindingFlags.SetField) != 0) {
445 FieldInfo f = GetField (name, invokeAttr);
448 throw new ArgumentNullException ("providedArgs");
449 if ((args == null) || args.Length != 1)
450 throw new ArgumentException ("Only the field value can be specified to set a field value.", bindingflags_arg);
451 f.SetValue (target, args [0]);
453 } else if ((invokeAttr & BindingFlags.SetProperty) == 0) {
454 throwMissingFieldException = true;
456 /* try SetProperty */
458 if ((invokeAttr & BindingFlags.GetProperty) != 0) {
459 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
462 for (i = 0; i < properties.Length; ++i) {
463 if ((properties [i].GetGetMethod (true) != null))
466 MethodBase[] smethods = new MethodBase [count];
468 for (i = 0; i < properties.Length; ++i) {
469 MethodBase mb = properties [i].GetGetMethod (true);
471 smethods [count++] = mb;
473 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
475 throwMissingFieldException = true;
477 object result = m.Invoke (target, invokeAttr, binder, args, culture);
478 binder.ReorderArgumentArray (ref args, state);
481 } else if ((invokeAttr & BindingFlags.SetProperty) != 0) {
482 PropertyInfo[] properties = GetPropertiesByName (name, invokeAttr, ignoreCase, this);
485 for (i = 0; i < properties.Length; ++i) {
486 if (properties [i].GetSetMethod (true) != null)
489 MethodBase[] smethods = new MethodBase [count];
491 for (i = 0; i < properties.Length; ++i) {
492 MethodBase mb = properties [i].GetSetMethod (true);
494 smethods [count++] = mb;
496 MethodBase m = binder.BindToMethod (invokeAttr, smethods, ref args, modifiers, culture, namedParameters, out state);
498 throwMissingFieldException = true;
500 object result = m.Invoke (target, invokeAttr, binder, args, culture);
501 binder.ReorderArgumentArray (ref args, state);
505 if (throwMissingMethodDescription != null)
506 throw new MissingMethodException(throwMissingMethodDescription);
507 if (throwMissingFieldException)
508 throw new MissingFieldException("Cannot find variable " + name + ".");
513 [MethodImplAttribute(MethodImplOptions.InternalCall)]
514 public extern override Type GetElementType ();
516 public override Type UnderlyingSystemType {
518 // This has _nothing_ to do with getting the base type of an enum etc.
523 public extern override Assembly Assembly {
524 [MethodImplAttribute(MethodImplOptions.InternalCall)]
528 public override string AssemblyQualifiedName {
530 return getFullName (true, true);
534 [MethodImplAttribute(MethodImplOptions.InternalCall)]
535 private extern string getFullName(bool full_name, bool assembly_qualified);
537 public extern override Type BaseType {
538 [MethodImplAttribute(MethodImplOptions.InternalCall)]
542 public override string FullName {
545 // This doesn't need locking
546 if (type_info == null)
547 type_info = new MonoTypeInfo ();
548 if ((fullName = type_info.full_name) == null)
549 fullName = type_info.full_name = getFullName (true, false);
555 public override Guid GUID {
557 object[] att = GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), true);
560 return new Guid(((System.Runtime.InteropServices.GuidAttribute)att[0]).Value);
564 public override bool IsDefined (Type attributeType, bool inherit)
566 return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
569 public override object[] GetCustomAttributes (bool inherit)
571 return MonoCustomAttrs.GetCustomAttributes (this, inherit);
574 public override object[] GetCustomAttributes (Type attributeType, bool inherit)
576 if (attributeType == null)
578 throw new ArgumentNullException("attributeType");
581 return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
584 public override MemberTypes MemberType {
586 if (DeclaringType != null && !IsGenericParameter)
587 return MemberTypes.NestedType;
589 return MemberTypes.TypeInfo;
593 public extern override string Name {
594 [MethodImplAttribute(MethodImplOptions.InternalCall)]
598 public extern override string Namespace {
599 [MethodImplAttribute(MethodImplOptions.InternalCall)]
603 public extern override Module Module {
604 [MethodImplAttribute(MethodImplOptions.InternalCall)]
608 public extern override Type DeclaringType {
609 [MethodImplAttribute(MethodImplOptions.InternalCall)]
613 public override Type ReflectedType {
615 return DeclaringType;
619 public override RuntimeTypeHandle TypeHandle {
625 [MethodImplAttribute(MethodImplOptions.InternalCall)]
626 public extern override int GetArrayRank ();
628 public void GetObjectData(SerializationInfo info, StreamingContext context)
630 UnitySerializationHolder.GetTypeData (this, info, context);
633 public override string ToString()
635 return getFullName (false, false);
638 [MethodImplAttribute(MethodImplOptions.InternalCall)]
639 public extern override Type [] GetGenericArguments ();
641 public override bool ContainsGenericParameters {
643 if (IsGenericParameter)
647 foreach (Type arg in GetGenericArguments ())
648 if (arg.ContainsGenericParameters)
653 return GetElementType ().ContainsGenericParameters;
659 public extern override bool IsGenericParameter {
660 [MethodImplAttribute(MethodImplOptions.InternalCall)]
664 public extern override MethodBase DeclaringMethod {
665 [MethodImplAttribute(MethodImplOptions.InternalCall)]
669 public override Type GetGenericTypeDefinition () {
670 Type res = GetGenericTypeDefinition_impl ();
672 throw new InvalidOperationException ();
678 public override IList<CustomAttributeData> GetCustomAttributesData () {
679 return CustomAttributeData.GetCustomAttributes (this);
683 public override Array GetEnumValues () {
685 throw new ArgumentException ("Type is not an enumeration", "enumType");
687 return Enum.GetValues (this);
691 static MethodBase CheckMethodSecurity (MethodBase mb)
696 if (!SecurityManager.SecurityEnabled || (mb == null))
699 // Sadly we have no way to know which kind of security action this is
700 // so we must do it the hard way. Actually this isn't so bad
701 // because we can skip the (mb.Attributes & MethodAttributes.HasSecurity)
702 // icall required (and do it ourselves)
704 // this (unlike the Invoke step) is _and stays_ a LinkDemand (caller)
705 return SecurityManager.ReflectedLinkDemandQuery (mb) ? mb : null;
710 //seclevel { transparent = 0, safe-critical = 1, critical = 2}
711 [MethodImplAttribute(MethodImplOptions.InternalCall)]
712 public extern int get_core_clr_security_level ();
714 public override bool IsSecurityTransparent
716 get { return get_core_clr_security_level () == 0; }
719 public override bool IsSecurityCritical
721 get { return get_core_clr_security_level () > 0; }
724 public override bool IsSecuritySafeCritical
726 get { return get_core_clr_security_level () == 1; }