var par = Expression.Parameter(typeof(water_flow1), "current");
var cloneExpression = Expression.MemberInit(
Expression.New(typeof(water_flow1)),
Enumerable.Range(0, typeof(water_flow1).GetProperties().Length - 1).Select(i =>
Expression.Bind(
typeof(water_flow1).GetProperties()[i],
Expression.Property(par, typeof(water_flow1).GetProperties()[i])
)
).ToArray()
);
var cloneFunc = Expression.Lambda<Func<water_flow1, water_flow1>>(cloneExpression, "Clone", new[] { par }).Compile();
public static TOut Copy<TIn, TOut>(TIn source) where TOut : new()
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TIn), "source");
MemberInitExpression memberInitExpression = Expression.MemberInit(
Expression.New(typeof(TOut)),
(from property in typeof(TOut).GetProperties()
where property.CanWrite
from sourceProperty in typeof(TIn).GetProperties()
where sourceProperty.Name == property.Name && sourceProperty.PropertyType == property.PropertyType
select Expression.Bind(property, Expression.Property(parameterExpression, sourceProperty))).ToArray()
);
Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, parameterExpression);
return lambda.Compile()(source);
}