Use IReflectType interface instead of TypeDelegator for introspection type extensions...
authorMarek Safar <marek.safar@gmail.com>
Wed, 28 Aug 2013 15:49:51 +0000 (17:49 +0200)
committerMarek Safar <marek.safar@gmail.com>
Wed, 28 Aug 2013 15:55:53 +0000 (17:55 +0200)
mcs/class/corlib/System.Reflection.Emit/EnumBuilder.cs
mcs/class/corlib/System.Reflection.Emit/GenericTypeParameterBuilder.cs
mcs/class/corlib/System.Reflection.Emit/TypeBuilder.cs
mcs/class/corlib/System.Reflection/IntrospectionExtensions.cs
mcs/class/corlib/System.Reflection/MonoGenericClass.cs
mcs/class/corlib/System.Reflection/TypeDelegator.cs
mcs/class/corlib/System.Reflection/TypeInfo.cs
mcs/class/corlib/System/MonoType.cs
mcs/class/corlib/Test/System.Reflection/IntrospectionExtensionsTest.cs [new file with mode: 0644]
mcs/class/corlib/corlib_test.dll.sources

index 50a674473779299209f339f4915230de6f31272a..240bc7297e7603106d3ce00e8f462eacd60d2ddb 100644 (file)
@@ -43,7 +43,14 @@ namespace System.Reflection.Emit {
        [ComVisible (true)]
        [ComDefaultInterface (typeof (_EnumBuilder))]
        [ClassInterface (ClassInterfaceType.None)]
-       public sealed class EnumBuilder : Type, _EnumBuilder {
+       public sealed class EnumBuilder : 
+#if NET_4_5
+               TypeInfo
+#else
+               Type
+#endif
+               , _EnumBuilder
+       {
                private TypeBuilder _tb;
                private FieldBuilder _underlyingField;
                private Type _underlyingType;
@@ -418,6 +425,18 @@ namespace System.Reflection.Emit {
                                return false;
                        }
                }
+
+#if NET_4_5
+               public override bool IsConstructedGenericType {
+                       get { return false; }
+               }
+
+               public override bool IsAssignableFrom (TypeInfo typeInfo)
+               {
+                       return base.IsAssignableFrom (typeInfo);
+               }
+#endif
+
        }
 }
 #endif
index 696ec9cef3a69e9f45eb30dd3d46704398152997..a4cfc9ca4fdd2985fc620038b0d4e006580f3609 100644 (file)
@@ -42,7 +42,12 @@ namespace System.Reflection.Emit
 {
        [ComVisible (true)]
        [StructLayout (LayoutKind.Sequential)]
-       public sealed class GenericTypeParameterBuilder : Type
+       public sealed class GenericTypeParameterBuilder : 
+#if NET_4_5
+               TypeInfo
+#else
+               Type
+#endif 
        {
        #region Sync with reflection.h
                private TypeBuilder tbuilder;
index 882d125442d15b6716eb7d686bbc6eeb52a1a03a..578a7ab057673f1f8f249cceb1e7199cbc8dd16f 100644 (file)
@@ -50,7 +50,13 @@ namespace System.Reflection.Emit
        [ComDefaultInterface (typeof (_TypeBuilder))]
        [ClassInterface (ClassInterfaceType.None)]
        [StructLayout (LayoutKind.Sequential)]
-       public sealed class TypeBuilder : Type, _TypeBuilder
+       public sealed class TypeBuilder :
+#if NET_4_5
+               TypeInfo
+#else
+               Type
+#endif
+               , _TypeBuilder
        {
 #pragma warning disable 169            
                #region Sync with reflection.h
@@ -1927,6 +1933,17 @@ namespace System.Reflection.Emit
                                return false;
                        }
                }
+
+#if NET_4_5
+               public override bool IsConstructedGenericType {
+                       get { return false; }
+               }
+
+               public override bool IsAssignableFrom (TypeInfo typeInfo)
+               {
+                       return base.IsAssignableFrom (typeInfo);
+               }
+#endif
        }
 }
 #endif
index 287366173363705e12ef32961ad2fdea6bc1198c..7f7cd76b9c5c1d6ad991d2992a024618daf038c4 100644 (file)
@@ -4,7 +4,7 @@
 // Authors:
 //     Marek Safar  <marek.safar@gmail.com>
 //
-// Copyright 2011 Xamarin Inc.
+// Copyright (C) 2013 Xamarin Inc (http://www.xamarin.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -34,7 +34,12 @@ namespace System.Reflection
        {
                public static TypeInfo GetTypeInfo (this Type type)
                {
-                       return new TypeDelegator (type);
+                       if (type == null)
+                               throw new ArgumentNullException ("type");
+
+                       // This is odd but that's what .net does
+                       var reflectable = (IReflectableType) type;
+                       return reflectable.GetTypeInfo ();
                }
        }
 }
index ad5e9436fee22d32349fc7aa8367d1f5e8b6f1e1..c053a4bbfc3e5195a4c22577524ba73d810dd099 100644 (file)
@@ -50,7 +50,12 @@ namespace System.Reflection
         * depends on them.
         */
        [StructLayout (LayoutKind.Sequential)]
-       internal class MonoGenericClass : Type
+       sealed class MonoGenericClass :
+#if NET_4_5
+               TypeInfo
+#else
+               Type
+#endif 
        {
                #region Keep in sync with object-internals.h
 #pragma warning disable 649
index 8ccb618ac8fd4186aeed21f3a0afdce9eb8338ba..01b4d58841d2c7ca2899cf7f8eec8747d22bb0bc 100644 (file)
@@ -260,5 +260,19 @@ namespace System.Reflection {
                        }
                }
 
+#if NET_4_5
+               public override bool IsConstructedGenericType {
+                       get { return typeImpl.IsConstructedGenericType; }
+               }
+
+               public override bool IsAssignableFrom (TypeInfo typeInfo)
+               {
+                       if (typeInfo == null)
+                               throw new ArgumentNullException ("typeInfo");
+
+                       return IsAssignableFrom (typeInfo.AsType ());
+               }
+#endif
+
        }
 }
index b5521116e3425983470d4a31304460d06d838c47..101aadc727e8986c46c92be2eb26ebe48ee519f9 100644 (file)
@@ -41,7 +41,7 @@ namespace System.Reflection
                        return this;
                }
 
-               static readonly BindingFlags declaredFlags = BindingFlags.DeclaredOnly |
+               const BindingFlags declaredFlags = BindingFlags.DeclaredOnly |
                        BindingFlags.Public | BindingFlags.NonPublic |
                                BindingFlags.Static | BindingFlags.Instance;
                
@@ -67,13 +67,7 @@ namespace System.Reflection
                
                public virtual IEnumerable<MemberInfo> DeclaredMembers {
                        get {
-                               var ret = new List<MemberInfo> ();
-                               ret.AddRange (DeclaredConstructors);
-                               ret.AddRange (DeclaredEvents);
-                               ret.AddRange (DeclaredFields);
-                               ret.AddRange (DeclaredMethods);
-                               ret.AddRange (DeclaredProperties);
-                               return ret.AsReadOnly ();
+                               return GetMembers (declaredFlags);
                        }
                }
                
@@ -87,28 +81,15 @@ namespace System.Reflection
                public virtual Type[] GenericTypeParameters {
                        get {
                                if (!ContainsGenericParameters)
-                                       return new Type [0];
+                                       return EmptyTypes;
+
                                return GetGenericArguments ();
                        }
                }
 
-               static bool list_contains (IEnumerable<Type> types, Type type)
-               {
-                       foreach (var t in types) {
-                               if (t == type)
-                                       return true;
-                       }
-
-                       return false;
-               }
-               
                public virtual IEnumerable<Type> ImplementedInterfaces {
                        get {
-                               var all = GetInterfaces ();
-                               var baseIfaces = BaseType != null ? BaseType.GetInterfaces () : new Type [0];
-                               foreach (var iface in all)
-                                       if (!list_contains (baseIfaces, iface))
-                                               yield return iface;
+                               return GetInterfaces ();
                        }
                }
 
index df90010c206114741e0eb77d6146df6a3c9c9acf..4bce62bd679c69e9abecdc2c11a3f96df6ba16f7 100644 (file)
@@ -51,7 +51,13 @@ namespace System
                
        [Serializable]
        [StructLayout (LayoutKind.Sequential)]
-       sealed class MonoType : Type, ISerializable
+       sealed class MonoType : 
+#if NET_4_5
+               TypeInfo
+#else
+               Type
+#endif
+               , ISerializable
        {
                [NonSerialized]
                MonoTypeInfo type_info;
diff --git a/mcs/class/corlib/Test/System.Reflection/IntrospectionExtensionsTest.cs b/mcs/class/corlib/Test/System.Reflection/IntrospectionExtensionsTest.cs
new file mode 100644 (file)
index 0000000..f16d9a2
--- /dev/null
@@ -0,0 +1,57 @@
+//
+// IntrospectionExtensionsTest.cs:
+//
+// Authors:
+//     Marek Safar (marek.safar@gmail.com)
+//
+// (C) 2013
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+#if NET_4_5
+
+using System;
+using System.Reflection;
+
+using NUnit.Framework;
+
+namespace MonoTests.System.Reflection
+{
+       [TestFixture]
+       public class IntrospectionExtensionsTest
+       {
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void GetTypeInfo_NullArgument ()
+               {
+                       IntrospectionExtensions.GetTypeInfo (null);
+               }
+               
+               [Test]
+               public void GetTypeInfo_SystemType ()
+               {
+                       var t = typeof (double);
+                       Assert.AreSame (t, IntrospectionExtensions.GetTypeInfo (t));
+               }
+       }
+}
+
+#endif
\ No newline at end of file
index 0ac74a6812b2a68db0826ae48e9781eb80bb29ac..51c354cc224645f7dbcba742086312519e6d66df 100644 (file)
@@ -133,6 +133,7 @@ System.Reflection/ConstructorInfoTest.cs
 System.Reflection/CustomAttributeDataTest.cs
 System.Reflection/EventInfoTest.cs
 System.Reflection/FieldInfoTest.cs
+System.Reflection/IntrospectionExtensionsTest.cs
 System.Reflection/MethodBaseTest.cs
 System.Reflection/MethodInfoTest.cs
 System.Reflection/MonoGenericClassTest.cs