AsyncEnumerable.AsyncOverloads.tt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. <#@ template debug="false" hostspecific="false" language="C#" #>
  5. <#@ assembly name="$(ProjectDir)\..\System.Linq.Async\bin\$(Configuration)\net46\System.Threading.Tasks.Extensions.dll" #>
  6. <#@ assembly name="$(ProjectDir)\..\System.Linq.Async\bin\$(Configuration)\net46\System.Linq.Async.dll" #>
  7. <#@ assembly name="System.Core" #>
  8. <#@ assembly name="System.Runtime" #>
  9. <#@ import namespace="System.Collections.Generic" #>
  10. <#@ import namespace="System.Linq" #>
  11. <#@ import namespace="System.Reflection" #>
  12. <#@ output extension=".cs" #>
  13. #if SUPPORT_FLAT_ASYNC_API
  14. using System.Collections.Generic;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. namespace System.Linq
  18. {
  19. partial class AsyncEnumerable
  20. {
  21. <#
  22. Func<Type, string> toCSharp = null;
  23. toCSharp = t =>
  24. {
  25. if (t.IsGenericType)
  26. {
  27. var def = t.GetGenericTypeDefinition();
  28. if (def == typeof(Nullable<>))
  29. {
  30. return toCSharp(t.GetGenericArguments()[0]) + "?";
  31. }
  32. else
  33. {
  34. return def.Name.Substring(0, def.Name.IndexOf('`')) + "<" + string.Join(", ", t.GetGenericArguments().Select(toCSharp)) + ">";
  35. }
  36. }
  37. else if (t.IsArray)
  38. {
  39. return toCSharp(t.GetElementType()) + "[]";
  40. }
  41. else
  42. {
  43. if (t == typeof(int))
  44. {
  45. return "int";
  46. }
  47. else if (t == typeof(long))
  48. {
  49. return "long";
  50. }
  51. else if (t == typeof(float))
  52. {
  53. return "float";
  54. }
  55. else if (t == typeof(double))
  56. {
  57. return "double";
  58. }
  59. else if (t == typeof(decimal))
  60. {
  61. return "decimal";
  62. }
  63. else if (t == typeof(bool))
  64. {
  65. return "bool";
  66. }
  67. else if (t == typeof(object))
  68. {
  69. return "object";
  70. }
  71. return t.Name;
  72. }
  73. };
  74. var methods = typeof(AsyncEnumerable).GetMethods(BindingFlags.Public | BindingFlags.Static);
  75. var asyncMethods = methods.Where(m => m.Name.Contains("Await")).OrderBy(m => m.Name).ThenBy(m => m.GetParameters().Length);
  76. foreach (var g in asyncMethods.GroupBy(m => m.Name.Contains("WithCancellation")))
  77. {
  78. if (g.Key)
  79. {
  80. #>
  81. #if !NO_DEEP_CANCELLATION
  82. <#
  83. }
  84. foreach (var m in g)
  85. {
  86. var longName = m.Name;
  87. var shortName = m.Name.Replace("WithCancellation", "").Replace("Await", "");
  88. var genArgs = m.IsGenericMethod ? "<" + string.Join(", ", m.GetGenericArguments().Select(t => t.Name)) + ">" : "";
  89. var returnType = toCSharp(m.ReturnType);
  90. var isExtension = m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true);
  91. var pars = (isExtension ? "this " : "") + string.Join(", ", m.GetParameters().Select(p => toCSharp(p.ParameterType) + " " + p.Name));
  92. var args = string.Join(", ", m.GetParameters().Select(p => p.Name));
  93. #>
  94. public static <#=returnType#> <#=shortName#><#=genArgs#>(<#=pars#>) => <#=longName#><#=genArgs#>(<#=args#>);
  95. <#
  96. }
  97. if (g.Key)
  98. {
  99. #>
  100. #endif
  101. <#
  102. }
  103. }
  104. #>
  105. }
  106. }
  107. #endif