Forked from leandromoh/Anonymous type with Expression.cs
Created
August 31, 2021 06:20
-
-
Save imgen/228914016be4f971f8b495c74abef5d7 to your computer and use it in GitHub Desktop.
Revisions
-
leandromoh renamed this gist
Jun 9, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
leandromoh created this gist
Jun 9, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ public static class AnonymousExtension { static void Main(string[] args) { var person = new { FirstName = "Scott", LastName = "Hunter", Sex = 'M', Age = 25 }; var otherPerson = person.With(new { LastName = "Hanselman" }); Console.WriteLine(otherPerson); } public static T With<T, U>(this T x, U y) { return AnonymousHelper<T, U>.Create(x, y); } private static class AnonymousHelper<T, U> { public static readonly Func<T, U, T> Create; static AnonymousHelper() { ParameterExpression x = Expression.Variable(typeof(T), "x"); ParameterExpression y = Expression.Variable(typeof(U), "y"); var ctr = typeof(T).GetConstructors()[0]; var values = ctr.GetParameters() .Select(p => { var instance = typeof(U).GetProperty(p.Name, p.ParameterType) is null ? x : y; return Expression.Property(instance, p.Name); }); var exp = Expression.New(ctr, values); var lam = Expression.Lambda<Func<T, U, T>>(exp, new[] { x, y }); Create = lam.Compile(); } } }