1
0
Эх сурвалжийг харах

Adding LocalQueryMethodImplementationTypeAttribute support.

Bart De Smet 8 жил өмнө
parent
commit
da30f71a27

+ 1 - 0
Ix.NET/Source/System.Interactive.Async.Providers/System/Linq/AsyncQueryableEx.cs

@@ -11,6 +11,7 @@ namespace System.Linq
     /// <summary>
     /// Provides a set of extension methods for asynchronous enumerable sequences represented using expression trees.
     /// </summary>
+    [LocalQueryMethodImplementationType(typeof(AsyncEnumerableEx))]
     public static partial class AsyncQueryableEx
     {
         private static Expression GetSourceExpression<TSource>(IAsyncEnumerable<TSource> source)

+ 6 - 1
Ix.NET/Source/System.Linq.Async.Queryable/System/Linq/AsyncEnumerableRewriter.cs

@@ -422,10 +422,15 @@ namespace System.Linq
 
         private static MethodInfo FindMethod(Type type, string name, ReadOnlyCollection<Expression> args, Type[] typeArgs, BindingFlags flags)
         {
+            //
+            // Support the enumerable methods to be defined on another type.
+            //
+            var targetType = type.GetTypeInfo().GetCustomAttribute<LocalQueryMethodImplementationTypeAttribute>()?.TargetType ?? type;
+
             //
             // Get all the candidates based on name and fail if none are found.
             //
-            var methods = type.GetMethods(flags).Where(m => m.Name == name).ToArray();
+            var methods = targetType.GetMethods(flags).Where(m => m.Name == name).ToArray();
             if (methods.Length == 0)
             {
                 throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Could not find method with name '{0}' on type '{1}'.", name, type));

+ 32 - 0
Ix.NET/Source/System.Linq.Async.Queryable/System/Linq/LocalQueryMethodImplementationTypeAttribute.cs

@@ -0,0 +1,32 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the Apache 2.0 License.
+// See the LICENSE file in the project root for more information. 
+
+using System.ComponentModel;
+
+namespace System.Linq
+{
+    /// <summary>
+    /// Attribute applied to static classes providing expression tree forms of query methods,
+    /// mapping those to the corresponding methods for local query execution on the specified
+    /// target class type.
+    /// </summary>
+    [EditorBrowsable(EditorBrowsableState.Never)]
+    [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
+    public sealed class LocalQueryMethodImplementationTypeAttribute : Attribute
+    {
+        /// <summary>
+        /// Creates a new mapping to the specified local execution query method implementation type.
+        /// </summary>
+        /// <param name="targetType">Type with query methods for local execution.</param>
+        public LocalQueryMethodImplementationTypeAttribute(Type targetType)
+        {
+            TargetType = targetType;
+        }
+
+        /// <summary>
+        /// Gets the type with the implementation of local query methods.
+        /// </summary>
+        public Type TargetType { get; }
+    }
+}