[LINQ] Fix non generic AsQueryable issue #661462
authorJb Evain <jbevain@gmail.com>
Thu, 30 Dec 2010 15:21:40 +0000 (16:21 +0100)
committerJb Evain <jbevain@gmail.com>
Thu, 30 Dec 2010 15:21:40 +0000 (16:21 +0100)
mcs/class/System.Core/System.Linq.Expressions/Extensions.cs
mcs/class/System.Core/System.Linq/Queryable.cs

index 28fb99a49f9bc3157367ff5c4d60136419318fae..3af0f5210f7d13dd5314880a2825400a65684706 100644 (file)
@@ -53,11 +53,17 @@ namespace System.Linq.Expressions {
                        return self == typeof (Expression) || self.IsSubclassOf (typeof (Expression));
                }
 
-               public static bool IsGenericImplementationOf (this Type self, Type type)
+               public static bool IsGenericImplementationOf (this Type self, Type type, out Type generic_iface)
                {
-                       foreach (Type iface in self.GetInterfaces ())
-                               if (iface.IsGenericInstanceOf (type))
-                                       return true;
+                       foreach (var iface in self.GetInterfaces ()) {
+                               if (!iface.IsGenericInstanceOf (type))
+                                       continue;
+
+                               generic_iface = iface;
+                               return true;
+                       }
+
+                       generic_iface = null;
                        return false;
                }
 
index 8c4a68fdb59bedda3d39b668e4d102e3c9bf53a4..b2fedba0c2fc353e5f41cacb429a88eb9625aa7e 100644 (file)
@@ -158,13 +158,12 @@ namespace System.Linq {
                        if (queryable != null)
                                return queryable;
 
-                       var type = source.GetType ();
-
-                       if (!type.IsGenericImplementationOf (typeof (IEnumerable<>)))
+                       Type ienumerable;
+                       if (!source.GetType ().IsGenericImplementationOf (typeof (IEnumerable<>), out ienumerable))
                                throw new ArgumentException ("source is not IEnumerable<>");
 
                        return (IQueryable) Activator.CreateInstance (
-                               typeof (QueryableEnumerable<>).MakeGenericType (type.GetFirstGenericArgument ()), source);
+                               typeof (QueryableEnumerable<>).MakeGenericType (ienumerable.GetFirstGenericArgument ()), source);
                }
 
                #endregion