AsyncQueryableEx.Generated.tt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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="System.Core" #>
  6. <#@ assembly name="$(ProjectDir)\..\System.Linq.Async\bin\$(Configuration)\net46\System.Linq.Async.dll" #>
  7. <#@ assembly name="$(ProjectDir)\..\System.Interactive.Async\bin\$(Configuration)\net46\System.Interactive.Async.dll" #>
  8. <#@ import namespace="System.Linq" #>
  9. <#@ import namespace="System.Text" #>
  10. <#@ import namespace="System.Threading" #>
  11. <#@ import namespace="System.Threading.Tasks" #>
  12. <#@ import namespace="System.Collections.Generic" #>
  13. <#@ output extension=".cs" #>
  14. <#
  15. var exclude = new[] { "ForEach", "ForEachAsync", "ToEnumerable", "ToAsyncEnumerable", "ToObservable", "AsAsyncEnumerable" };
  16. var toQuotedImpl = default(Func<Type, int, bool, string>);
  17. toQuotedImpl = (t, i, b) =>
  18. {
  19. var name = t.Name;
  20. if (t.IsGenericType)
  21. {
  22. var genDef = t.GetGenericTypeDefinition();
  23. name = genDef.Name.Substring(0, genDef.Name.LastIndexOf('`'));
  24. var genArgs = "<" + string.Join(", ", t.GetGenericArguments().Select(a => toQuotedImpl(a, i, false))) + ">";
  25. if (b)
  26. {
  27. if (name == "Func" || name == "Action")
  28. {
  29. name = "Expression<" + name + genArgs + ">";
  30. }
  31. else if (name == "IAsyncEnumerable" && i == 0)
  32. {
  33. name = "IAsyncQueryable" + genArgs;
  34. }
  35. else if (name == "IOrderedAsyncEnumerable" && i == 0)
  36. {
  37. name = "IOrderedAsyncQueryable" + genArgs;
  38. }
  39. else
  40. {
  41. name += genArgs;
  42. }
  43. }
  44. else
  45. {
  46. if (name == "Nullable")
  47. {
  48. name = genArgs.Substring(1, genArgs.Length - 2) + "?";
  49. }
  50. else
  51. {
  52. name += genArgs;
  53. }
  54. }
  55. }
  56. else if (t.IsArray)
  57. {
  58. var elem = toQuotedImpl(t.GetElementType(), i, b);
  59. name = elem + "[]";
  60. }
  61. else
  62. {
  63. if (t == typeof(int))
  64. {
  65. name = "int";
  66. }
  67. else if (t == typeof(long))
  68. {
  69. name = "long";
  70. }
  71. else if (t == typeof(float))
  72. {
  73. name = "float";
  74. }
  75. else if (t == typeof(double))
  76. {
  77. name = "double";
  78. }
  79. else if (t == typeof(decimal))
  80. {
  81. name = "decimal";
  82. }
  83. else if (t == typeof(bool))
  84. {
  85. name = "bool";
  86. }
  87. else if (t == typeof(object))
  88. {
  89. name = "object";
  90. }
  91. }
  92. return name;
  93. };
  94. var toQuoted = new Func<Type, int, string>((t, i) => toQuotedImpl(t, i, true));
  95. #>
  96. using System.Collections.Generic;
  97. using System.Linq.Expressions;
  98. using System.Reflection;
  99. using System.Threading;
  100. using System.Threading.Tasks;
  101. namespace System.Linq
  102. {
  103. public static partial class AsyncQueryableEx
  104. {
  105. <#
  106. // NOTE: Just including extension methods
  107. foreach (var m in typeof(AsyncEnumerableEx).GetMethods()
  108. .Where(m => m.IsStatic)
  109. .Where(m => !exclude.Contains(m.Name))
  110. .Where(m => m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true))
  111. .Where(m =>
  112. {
  113. var p0 = m.GetParameters()[0].ParameterType;
  114. if (p0.IsGenericType)
  115. {
  116. var p0d = p0.GetGenericTypeDefinition();
  117. return p0d == typeof(IAsyncEnumerable<>) || p0d == typeof(IOrderedAsyncEnumerable<>);
  118. }
  119. return false;
  120. })
  121. .OrderBy(m => m.Name)
  122. .ThenBy(m => m.GetParameters().Length))
  123. {
  124. var genArgs = m.GetGenericArguments();
  125. var ret = toQuoted(m.ReturnType, 0);
  126. var name = m.Name;
  127. if (genArgs.Length > 0)
  128. {
  129. name += "<" + string.Join(", ", genArgs.Select(a => a.Name)) + ">";
  130. }
  131. var isParams = false;
  132. var parCount = m.GetParameters().Length;
  133. if (parCount != 0)
  134. {
  135. if (m.GetParameters().Last().IsDefined(typeof(ParamArrayAttribute), true))
  136. {
  137. isParams = true;
  138. }
  139. }
  140. var pars = string.Join(", ", m.GetParameters().Select((p, i) => (i == parCount - 1 && isParams ? "params " : "") + toQuoted(p.ParameterType, i) + " " + p.Name));
  141. var quotedPars = string.Join(", ", m.GetParameters().Select((p, i) => "default(" + toQuoted(p.ParameterType, i) + ")"));
  142. if (m.IsDefined(typeof(System.Runtime.CompilerServices.ExtensionAttribute), true))
  143. {
  144. pars = "this " + pars;
  145. }
  146. var mtd = "(MethodInfo)MethodBase.GetCurrentMethod()";
  147. if (m.IsGenericMethod)
  148. {
  149. mtd = "(" + mtd + ").MakeGenericMethod(" + string.Join(", ", genArgs.Select(a => "typeof(" + a.Name + ")")) + ")";
  150. }
  151. var provider = m.GetParameters()[0].Name + ".Provider";
  152. var factory = "";
  153. var rem = "";
  154. var cast = "";
  155. var quotedArgs = new List<string>();
  156. if (m.ReturnType.IsGenericType)
  157. {
  158. var td = m.ReturnType.GetGenericTypeDefinition();
  159. if (td == typeof(Task<>))
  160. {
  161. factory = "ExecuteAsync<" + toQuotedImpl(m.ReturnType.GetGenericArguments()[0], -1, false) + ">";
  162. var last = m.GetParameters().Last();
  163. if (last.ParameterType == typeof(CancellationToken))
  164. {
  165. rem = ", " + last.Name;
  166. }
  167. else
  168. {
  169. rem = ", CancellationToken.None";
  170. }
  171. }
  172. else if (td == typeof(IAsyncEnumerable<>) || td == typeof(IOrderedAsyncEnumerable<>))
  173. {
  174. factory = "CreateQuery<" + toQuotedImpl(m.ReturnType.GetGenericArguments()[0], -1, false) + ">";
  175. if (td == typeof(IOrderedAsyncEnumerable<>))
  176. {
  177. cast = "(" + toQuoted(m.ReturnType, 0) + ")";
  178. }
  179. }
  180. }
  181. var n = 0;
  182. foreach (var p in m.GetParameters())
  183. {
  184. var pt = p.ParameterType;
  185. var add = false;
  186. if (pt.IsGenericType)
  187. {
  188. var ptd = pt.GetGenericTypeDefinition();
  189. if (ptd == typeof(IAsyncEnumerable<>) || ptd == typeof(IOrderedAsyncEnumerable<>))
  190. {
  191. if (n == 0)
  192. {
  193. quotedArgs.Add(p.Name + ".Expression");
  194. }
  195. else
  196. {
  197. quotedArgs.Add("GetSourceExpression(" + p.Name + ")");
  198. }
  199. add = true;
  200. }
  201. else if (ptd.Name.StartsWith("Func") || ptd.Name.StartsWith("Action"))
  202. {
  203. quotedArgs.Add(p.Name);
  204. add = true;
  205. }
  206. }
  207. if (!add)
  208. {
  209. quotedArgs.Add("Expression.Constant(" + p.Name + ", typeof(" + toQuoted(pt, -1) + "))");
  210. }
  211. n++;
  212. }
  213. var expr = "Expression.Call(" + mtd + ", " + string.Join(", ", quotedArgs) + ")";
  214. var infoofmtd = "InfoOf(() => AsyncQueryableEx." + name + "(" + quotedPars + "))";
  215. var infoofexpr = "Expression.Call(" + infoofmtd + ", " + string.Join(", ", quotedArgs) + ")";
  216. #>
  217. public static <#=ret#> <#=name#>(<#=pars#>)
  218. {
  219. <#
  220. var any = false;
  221. foreach (var p in m.GetParameters())
  222. {
  223. if (!p.ParameterType.IsValueType && !p.ParameterType.IsGenericParameter)
  224. {
  225. any = true;
  226. #>
  227. if (<#=p.Name#> == null)
  228. throw new ArgumentNullException(nameof(<#=p.Name#>));
  229. <#
  230. }
  231. }
  232. #>
  233. <#
  234. if (any)
  235. {
  236. #>
  237. <#
  238. }
  239. #>
  240. #if CRIPPLED_REFLECTION
  241. return <#=cast#><#=provider#>.<#=factory#>(<#=infoofexpr#><#=rem#>);
  242. #else
  243. return <#=cast#><#=provider#>.<#=factory#>(<#=expr#><#=rem#>);
  244. #endif
  245. }
  246. <#
  247. }
  248. #>
  249. }
  250. }