2008-07-24 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mcs / class / corlib / System / Type.cs
index 7e37c635f9cf09efce382fa575a86237fe9e1a14..fcf8986ef0bb59da56d50cc99d7a2f4fc1ea6517 100644 (file)
@@ -64,7 +64,7 @@ namespace System {
                static bool FilterName_impl (MemberInfo m, object filterCriteria)
                {
                        string name = (string) filterCriteria;
-               if (name == null || name.Length == 0 )
+                       if (name == null || name.Length == 0 )
                                return false; // because m.Name cannot be null or empty
                                
                        if (name [name.Length-1] == '*')
@@ -76,7 +76,7 @@ namespace System {
                static bool FilterNameIgnoreCase_impl (MemberInfo m, object filterCriteria)
                {
                        string name = (string) filterCriteria;
-               if (name == null || name.Length == 0 )
+                       if (name == null || name.Length == 0 )
                                return false; // because m.Name cannot be null or empty
                                
                        if (name [name.Length-1] == '*')
@@ -413,6 +413,10 @@ namespace System {
                        }
                }
 
+               /*
+                * This has NOTHING to do with getting the base type of an enum. Use
+                * Enum.GetUnderlyingType () for that.
+                */
                public abstract Type UnderlyingSystemType {get;}
 
                public override bool Equals (object o)
@@ -426,10 +430,10 @@ namespace System {
                        return Equals (cmp);
                }
 
-               public bool Equals (Type type) {
-                       if (type == null)
+               public bool Equals (Type o) {
+                       if (o == null)
                                return false;
-                       return UnderlyingSystemType.EqualsInternal (type.UnderlyingSystemType);
+                       return UnderlyingSystemType.EqualsInternal (o.UnderlyingSystemType);
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
@@ -444,7 +448,11 @@ namespace System {
                public static Type GetType(string typeName)
                {
                        if (typeName == null)
-                               throw new ArgumentNullException ("typeName");
+#if NET_2_0
+                               throw new ArgumentNullException ("TypeName");
+#else
+                               throw new ArgumentNullException ("className");
+#endif
 
                        return internal_from_name (typeName, false, false);
                }
@@ -452,7 +460,11 @@ namespace System {
                public static Type GetType(string typeName, bool throwOnError)
                {
                        if (typeName == null)
-                               throw new ArgumentNullException ("typeName");
+#if NET_2_0
+                               throw new ArgumentNullException ("TypeName");
+#else
+                               throw new ArgumentNullException ("className");
+#endif
 
                        Type type = internal_from_name (typeName, throwOnError, false);
                        if (throwOnError && type == null)
@@ -464,7 +476,11 @@ namespace System {
                public static Type GetType(string typeName, bool throwOnError, bool ignoreCase)
                {
                        if (typeName == null)
-                               throw new ArgumentNullException ("typeName");
+#if NET_2_0
+                               throw new ArgumentNullException ("TypeName");
+#else
+                               throw new ArgumentNullException ("className");
+#endif
 
                        Type t = internal_from_name (typeName, throwOnError, ignoreCase);
                        if (throwOnError && t == null)
@@ -488,6 +504,8 @@ namespace System {
                internal extern static TypeCode GetTypeCodeInternal (Type type);
 
                public static TypeCode GetTypeCode (Type type) {
+                       if (type is MonoType)
+                               return GetTypeCodeInternal (type);
                        if (type == null)
                                /* MS.NET returns this */
                                return TypeCode.Empty;
@@ -495,7 +513,7 @@ namespace System {
                        type = type.UnderlyingSystemType;
 
                        if (!type.IsSystemType)
-                               return Type.GetTypeCode (typeof (object));
+                               return TypeCode.Object;
                        else
                                return GetTypeCodeInternal (type);
                }
@@ -525,7 +543,16 @@ namespace System {
                }
 
                public static Type GetTypeFromHandle (RuntimeTypeHandle handle)
-               { 
+               {
+                       if (handle.Value == IntPtr.Zero)
+#if NET_2_0
+                               // This is not consistent with the other GetXXXFromHandle methods, but
+                               // MS.NET seems to do this
+                               return null;
+#else
+                               throw new ArgumentException ("The handle is invalid.");
+#endif
+
                        return internal_from_handle (handle.Value);
                }
 
@@ -555,6 +582,13 @@ namespace System {
 
                public static RuntimeTypeHandle GetTypeHandle (object o)
                {
+                       if (o == null)
+#if NET_2_0
+                               throw new ArgumentNullException ();
+#else
+                               throw new ArgumentNullException ("o");
+#endif
+
                        return o.GetType().TypeHandle;
                }
 
@@ -757,7 +791,6 @@ namespace System {
                public MethodInfo GetMethod (string name, BindingFlags bindingAttr, Binder binder,
                                             Type[] types, ParameterModifier[] modifiers)
                {
-                       
                        return GetMethod (name, bindingAttr, binder, CallingConventions.Any, types, modifiers);
                }
 
@@ -935,7 +968,7 @@ namespace System {
 #endif
                public ConstructorInfo GetConstructor (Type[] types)
                {
-                       return GetConstructor (DefaultBindingFlags, null, CallingConventions.Any, types, null);
+                       return GetConstructor (BindingFlags.Public|BindingFlags.Instance, null, CallingConventions.Any, types, null);
                }
 
 #if NET_2_0
@@ -1143,25 +1176,27 @@ namespace System {
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                static extern Type MakeGenericType (Type gt, Type [] types);
 
-               public virtual Type MakeGenericType (params Type[] types)
+               public virtual Type MakeGenericType (params Type[] typeArguments)
                {
                        if (!IsGenericTypeDefinition)
                                throw new InvalidOperationException ("not a generic type definition");
-                       if (types == null)
-                               throw new ArgumentNullException ("types");
-
-                       Type[] systemTypes = new Type[types.Length];
-                       for (int i = 0; i < types.Length; ++i) {
-                               Type t = types [i];
+                       if (typeArguments == null)
+                               throw new ArgumentNullException ("typeArguments");
+                       if (GetGenericArguments().Length != typeArguments.Length)
+                               throw new ArgumentException (String.Format ("The type or method has {0} generic parameter(s) but {1} generic argument(s) where provided. A generic argument must be provided for each generic parameter.", GetGenericArguments ().Length, typeArguments.Length), "typeArguments");
+
+                       Type[] systemTypes = new Type[typeArguments.Length];
+                       for (int i = 0; i < typeArguments.Length; ++i) {
+                               Type t = typeArguments [i];
                                if (t == null)
-                                       throw new ArgumentNullException ("types");
+                                       throw new ArgumentNullException ("typeArguments");
 
                                t = t.UnderlyingSystemType;
                                if (t == null || !t.IsSystemType)
-                                       throw new ArgumentNullException ("types");
-                               systemTypes [i] = types [i].UnderlyingSystemType;
+                                       throw new ArgumentNullException ("typeArguments");
+                               systemTypes [i] = typeArguments [i].UnderlyingSystemType;
                        }
-                       
+
                        Type res = MakeGenericType (this, systemTypes);
                        if (res == null)
                                throw new TypeLoadException ();
@@ -1269,7 +1304,7 @@ namespace System {
                        Assembly a;
                        try {
                                a = Assembly.ReflectionOnlyLoad (an);
-                       } catch (Exception ex) {
+                       } catch {
                                if (throwIfNotFound)
                                        throw;
                                return null;