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.
Anonymous extension method that allow create a new instance of anonymous with new values, similar to with expression of records in C# and F#
This file contains 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 characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment