Versuch, einen Ausdrucksbaum zu erstellen, um eine Objekt-Mapper-Art zu tun.Expression Tree-Kopierobjekt
Type ts = typeof(Source);
Type td = typeof(Dest);
ParameterExpression val = Expression.Parameter(ts);
ParameterExpression ret = Expression.Parameter(td);
PropertyInfo[] propsS = ts.GetProperties();
PropertyInfo[] propsD = td.GetProperties();
List<Expression> lst = new List<Expression>();
foreach (PropertyInfo pi in propsS)
{
PropertyInfo piD = propsD.Where(x => x.Name == pi.Name).FirstOrDefault();
if (piD != null)
{
MethodInfo ge = pi.GetGetMethod();
MethodInfo se = piD.GetSetMethod();
var v1 = Expression.Call(val, ge);
var v2 = Expression.Call(ret, se, v1);
lst.Add(v2);
}
}
lst.Add(Expression.Return(Expression.Label(td), ret));
BlockExpression block = Expression.Block(
new[] { ret },
lst.ToArray()
);
//Func<Source, Dest> v = Expression.Lambda<Func<Source, Dest>>(block, val).Compile();
var v = Expression.Lambda(block, val);
Also das ist, was ich jetzt habe ... seine ganz in der Nähe, aber nicht sehen, was mir fehlt ...
v kommt auf:
.Lambda #Lambda1<System.Action`1[ConsoleApplication2.Source]>(ConsoleApplication2.Source $var1) {
.Block(ConsoleApplication2.Dest $var2) {
.Call $var2.set_S1(.Call $var1.get_S1());
.Call $var2.set_S2(.Call $var1.get_S2());
.Call $var2.set_I1(.Call $var1.get_I1());
.Call $var2.set_I2(.Call $var1.get_I2());
.Call $var2.set_S3(.Call $var1.get_S3());
.Call $var2.set_S4(.Call $var1.get_S4());
.Call $var2.set_S5(.Call $var1.get_S5());
.Return #Label1 { $var2 }
}
}
- Muss ich $ var2 irgendwo neu aufbauen?
- Gibt es eine bessere Möglichkeit, die Zuweisungen zu tun?
- Der Lambda scheint nicht den Rückgabewert zu sehen ...
- Muss ich den Block tun? oder gibt es einen besseren Weg?
Entweder muss das zurückgegebene Lambda 'Func
@Rob - Ich möchte das Func