123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- // 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.
- <#@ template debug="false" hostspecific="false" language="C#" #>
- <#@ assembly name="$(ProjectDir)\..\System.Linq.Async\bin\$(Configuration)\net46\System.Threading.Tasks.Extensions.dll" #>
- <#@ assembly name="$(ProjectDir)\..\System.Linq.Async\bin\$(Configuration)\net46\System.Linq.Async.dll" #>
- <#@ assembly name="System.Core" #>
- <#@ assembly name="System.Runtime" #>
- <#@ import namespace="System.Collections.Generic" #>
- <#@ import namespace="System.Linq" #>
- <#@ import namespace="System.Reflection" #>
- <#@ output extension=".cs" #>
- <#
- Func<Type, string> toCSharp = null;
- toCSharp = t =>
- {
- if (t.IsGenericType)
- {
- var def = t.GetGenericTypeDefinition();
- if (def == typeof(Nullable<>))
- {
- return toCSharp(t.GetGenericArguments()[0]) + "?";
- }
- else
- {
- return def.Name.Substring(0, def.Name.IndexOf('`')) + "<" + string.Join(", ", t.GetGenericArguments().Select(toCSharp)) + ">";
- }
- }
- else if (t.IsArray)
- {
- return toCSharp(t.GetElementType()) + "[]";
- }
- else
- {
- if (t == typeof(int))
- {
- return "int";
- }
- else if (t == typeof(long))
- {
- return "long";
- }
- else if (t == typeof(float))
- {
- return "float";
- }
- else if (t == typeof(double))
- {
- return "double";
- }
- else if (t == typeof(decimal))
- {
- return "decimal";
- }
- else if (t == typeof(bool))
- {
- return "bool";
- }
- else if (t == typeof(object))
- {
- return "object";
- }
- return t.Name;
- }
- };
- Func<ParameterInfo, string> getOptionalSuffix = p =>
- {
- if (p.IsOptional)
- {
- return " = default";
- }
- return "";
- };
- #>
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- partial class AsyncEnumerable
- {
- <#
- var methods = typeof(AsyncEnumerable).GetMethods(BindingFlags.NonPublic | BindingFlags.Static);
- var asyncMethods = methods.Where(m => m.Name.Contains("Await") && m.Name.EndsWith("Core")).OrderBy(m => m.Name).ThenBy(m => m.GetParameters().Length);
- #>
- #if SUPPORT_FLAT_ASYNC_API
- <#
- foreach (var g in asyncMethods.GroupBy(m => m.Name.Contains("WithCancellation")))
- {
- if (g.Key)
- {
- #>
- #if !NO_DEEP_CANCELLATION
- <#
- }
- foreach (var m in g)
- {
- var longName = m.Name;
- var shortName = m.Name.Replace("WithCancellation", "").Replace("Await", "").Replace("Core", "");
- var genArgs = m.IsGenericMethod ? "<" + string.Join(", ", m.GetGenericArguments().Select(t => t.Name)) + ">" : "";
- var returnType = toCSharp(m.ReturnType);
- var isExtension = m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true);
- var pars = (isExtension ? "this " : "") + string.Join(", ", m.GetParameters().Select(p => toCSharp(p.ParameterType) + " " + p.Name + getOptionalSuffix(p)));
- var args = string.Join(", ", m.GetParameters().Select(p => p.Name));
- var cons = m.Name.StartsWith("ToDictionary") ? " where TKey : notnull" : "";
- #>
- public static <#=returnType#> <#=shortName#><#=genArgs#>(<#=pars#>)<#=cons#> => <#=longName#><#=genArgs#>(<#=args#>);
- <#
- }
- if (g.Key)
- {
- #>
- #endif
- <#
- }
- }
- #>
- #else
- <#
- foreach (var g in asyncMethods.GroupBy(m => m.Name.Contains("WithCancellation")))
- {
- if (g.Key)
- {
- #>
- #if !NO_DEEP_CANCELLATION
- <#
- }
- foreach (var m in g)
- {
- var longName = m.Name;
- var shortName = m.Name.Replace("Core", "");
- var genArgs = m.IsGenericMethod ? "<" + string.Join(", ", m.GetGenericArguments().Select(t => t.Name)) + ">" : "";
- var returnType = toCSharp(m.ReturnType);
- var isExtension = m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true);
- var pars = (isExtension ? "this " : "") + string.Join(", ", m.GetParameters().Select(p => toCSharp(p.ParameterType) + " " + p.Name + getOptionalSuffix(p)));
- var args = string.Join(", ", m.GetParameters().Select(p => p.Name));
- var cons = m.Name.StartsWith("ToDictionary") ? " where TKey : notnull" : "";
- #>
- public static <#=returnType#> <#=shortName#><#=genArgs#>(<#=pars#>)<#=cons#> => <#=longName#><#=genArgs#>(<#=args#>);
- <#
- }
- if (g.Key)
- {
- #>
- #endif
- <#
- }
- }
- #>
- #endif
- }
- }
|