diff --git a/src/Tgstation.Server.Host/Models/CollectionTransformer{TInput,TOutput,TTransformer}.cs b/src/Tgstation.Server.Host/Models/CollectionTransformer{TInput,TOutput,TTransformer}.cs new file mode 100644 index 0000000000..b11ad88378 --- /dev/null +++ b/src/Tgstation.Server.Host/Models/CollectionTransformer{TInput,TOutput,TTransformer}.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; + +namespace Tgstation.Server.Host.Models +{ + /// + /// for transforming collections. + /// + /// The input collection's underlying . + /// The output collection's underlying . + /// The from to . + sealed class CollectionTransformer : TransformerBase, List> + where TTransformer : ITransformer, new() + { + /// + /// Build the . + /// + /// The for converting collections of s to s. + private static Expression, List>> BuildExpression() + { + var subTransformer = new TTransformer(); + Expression, Func, List>> expression = (model, subExpression) => model + .Select(subExpression) + .ToList(); + + var parameter = global::System.Linq.Expressions.Expression.Parameter(typeof(IEnumerable), "collectionInput"); + var invocation = global::System.Linq.Expressions.Expression.Invoke(expression, parameter, subTransformer.Expression); + + var lambda = global::System.Linq.Expressions.Expression.Lambda, List>>(invocation, parameter); + + return lambda; + } + + /// + /// Initializes a new instance of the class. + /// + public CollectionTransformer() + : base(BuildExpression()) + { + } + } +}