AsyncEnumerable.AsyncOverloads.tt 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. <#
  14. Func<Type, string> toCSharp = null;
  15. toCSharp = t =>
  16. {
  17. if (t.IsGenericType)
  18. {
  19. var def = t.GetGenericTypeDefinition();
  20. if (def == typeof(Nullable<>))
  21. {
  22. return toCSharp(t.GetGenericArguments()[0]) + "?";
  23. }
  24. else
  25. {
  26. return def.Name.Substring(0, def.Name.IndexOf('`')) + "<" + string.Join(", ", t.GetGenericArguments().Select(toCSharp)) + ">";
  27. }
  28. }
  29. else if (t.IsArray)
  30. {
  31. return toCSharp(t.GetElementType()) + "[]";
  32. }
  33. else
  34. {
  35. if (t == typeof(int))
  36. {
  37. return "int";
  38. }
  39. else if (t == typeof(long))
  40. {
  41. return "long";
  42. }
  43. else if (t == typeof(float))
  44. {
  45. return "float";
  46. }
  47. else if (t == typeof(double))
  48. {
  49. return "double";
  50. }
  51. else if (t == typeof(decimal))
  52. {
  53. return "decimal";
  54. }
  55. else if (t == typeof(bool))
  56. {
  57. return "bool";
  58. }
  59. else if (t == typeof(object))
  60. {
  61. return "object";
  62. }
  63. return t.Name;
  64. }
  65. };
  66. Func<ParameterInfo, string> getOptionalSuffix = p =>
  67. {
  68. if (p.IsOptional)
  69. {
  70. return " = default";
  71. }
  72. return "";
  73. };
  74. #>
  75. using System.Collections.Generic;
  76. using System.Threading;
  77. using System.Threading.Tasks;
  78. namespace System.Linq
  79. {
  80. partial class AsyncEnumerable
  81. {
  82. <#
  83. var methods = typeof(AsyncEnumerable).GetMethods(BindingFlags.NonPublic | BindingFlags.Static);
  84. var asyncMethods = methods.Where(m => m.Name.Contains("Await") && m.Name.EndsWith("Core")).OrderBy(m => m.Name).ThenBy(m => m.GetParameters().Length);
  85. #>
  86. #if SUPPORT_FLAT_ASYNC_API
  87. <#
  88. foreach (var g in asyncMethods.GroupBy(m => m.Name.Contains("WithCancellation")))
  89. {
  90. if (g.Key)
  91. {
  92. #>
  93. #if !NO_DEEP_CANCELLATION
  94. <#
  95. }
  96. foreach (var m in g)
  97. {
  98. var longName = m.Name;
  99. var shortName = m.Name.Replace("WithCancellation", "").Replace("Await", "").Replace("Core", "");
  100. var genArgs = m.IsGenericMethod ? "<" + string.Join(", ", m.GetGenericArguments().Select(t => t.Name)) + ">" : "";
  101. var returnType = toCSharp(m.ReturnType);
  102. var isExtension = m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true);
  103. var pars = (isExtension ? "this " : "") + string.Join(", ", m.GetParameters().Select(p => toCSharp(p.ParameterType) + " " + p.Name + getOptionalSuffix(p)));
  104. var args = string.Join(", ", m.GetParameters().Select(p => p.Name));
  105. var cons = m.Name.StartsWith("ToDictionary") ? " where TKey : notnull" : "";
  106. #>
  107. public static <#=returnType#> <#=shortName#><#=genArgs#>(<#=pars#>)<#=cons#> => <#=longName#><#=genArgs#>(<#=args#>);
  108. <#
  109. }
  110. if (g.Key)
  111. {
  112. #>
  113. #endif
  114. <#
  115. }
  116. }
  117. #>
  118. #else
  119. <#
  120. foreach (var g in asyncMethods.GroupBy(m => m.Name.Contains("WithCancellation")))
  121. {
  122. if (g.Key)
  123. {
  124. #>
  125. #if !NO_DEEP_CANCELLATION
  126. <#
  127. }
  128. foreach (var m in g)
  129. {
  130. var longName = m.Name;
  131. var shortName = m.Name.Replace("Core", "");
  132. var genArgs = m.IsGenericMethod ? "<" + string.Join(", ", m.GetGenericArguments().Select(t => t.Name)) + ">" : "";
  133. var returnType = toCSharp(m.ReturnType);
  134. var isExtension = m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true);
  135. var pars = (isExtension ? "this " : "") + string.Join(", ", m.GetParameters().Select(p => toCSharp(p.ParameterType) + " " + p.Name + getOptionalSuffix(p)));
  136. var args = string.Join(", ", m.GetParameters().Select(p => p.Name));
  137. var cons = m.Name.StartsWith("ToDictionary") ? " where TKey : notnull" : "";
  138. #>
  139. public static <#=returnType#> <#=shortName#><#=genArgs#>(<#=pars#>)<#=cons#> => <#=longName#><#=genArgs#>(<#=args#>);
  140. <#
  141. }
  142. if (g.Key)
  143. {
  144. #>
  145. #endif
  146. <#
  147. }
  148. }
  149. #>
  150. #endif
  151. }
  152. }