Questo è il mio semplice codice di prova. Voglio creare un collegamento di assegnazione di campo tra 2 oggetti, e il campo è determinato in fase di esecuzione utilizzando il reflection memorizzando nella cache il delegato del suo metodo setter / getter. Ma, in qualche modo, non funziona. Il compito non funziona; forse ho fatto un errore stupido. Dove sono sbagliato?
public static class AssignmentExpression
{
public static Expression Create(Expression left, Expression right)
{
MethodInfo m = typeof(AssignmentExpression)
.GetMethod("AssignTo", BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(left.Type);
return Expression.Call( null,m,left, right);
}
private static void AssignTo<T>(ref T left, T right)
{
left = right;
}
}
public class FieldLink
{
protected Delegate srcGetter;
protected Delegate dstSetter;
public FieldLink(FieldInfo srcObject, FieldInfo dstObject)
{
this.srcGetter = FieldLink.createGetter(srcObject);
this.dstSetter = FieldLink.createSetter(dstObject);
}
public void update<T>(T dst, T src)
{
this.dstSetter.DynamicInvoke(dst, this.srcGetter.DynamicInvoke(src));
}
protected static Delegate createGetter(FieldInfo field)
{
ParameterExpression objParm = Expression.Parameter(field.DeclaringType, "obj");
Type delegateType = typeof(Func<,>).MakeGenericType(field.DeclaringType, field.FieldType);
MemberExpression fieldExpr = Expression.Field(objParm, field.Name);
LambdaExpression lambda = Expression.Lambda(delegateType, fieldExpr, objParm);
return lambda.Compile();
}
protected static Delegate createSetter(FieldInfo field)
{
ParameterExpression objParm = Expression.Parameter(field.DeclaringType, "obj");
ParameterExpression valueParm = Expression.Parameter(field.FieldType, "value");
Type delegateType = typeof(Action<,>).MakeGenericType(field.DeclaringType, field.FieldType);
MemberExpression memberExpr = Expression.Field(objParm, field.Name);
Expression assignExpr = AssignmentExpression.Create(memberExpr, valueParm);
LambdaExpression lambda = Expression.Lambda(delegateType, assignExpr, objParm, valueParm);
return lambda.Compile();
}
}
public class Test
{
public int fieldInt = 0;
}
public class TestClass
{
public Test a = new Test();
public Test b = new Test();
public void Start()
{
a.fieldInt = 5;
Debug.Log("before a = " + a.fieldInt + " b = " + b.fieldInt);
FieldLink testLink = new FieldLink(this.a.GetType().GetField("fieldInt"),
this.b.GetType().GetField("fieldInt"));
testLink.update(this.b, this.a);
Debug.Log("after a = " + a.fieldInt + " b = " + b.fieldInt);
//here a.fieldInt should be equal to b.fieldInt, but somehow its unchanged!
}
}
Il tuo codice sembra funzionare, ma forse non esattamente come ti aspetti. Quando chiamate l' update
, passate in b.fieldInt
come argomento di left
e a.fieldInt
come argomento di right
; il metodo di update
assegna quindi il valore di a.fieldInt
(5) al campo b.fieldInt
, con il risultato che entrambi gli oggetti hanno un valore fieldInt
di 5. Se si invertono gli argomenti, entrambi i campi finiscono come zero. Non è quello che ti aspetti?
Per inciso, e forse hai altri motivi per utilizzare la tua classe AssignmentExpression
- ma puoi effettivamente sostituire:
Expression assignExpr = AssignmentExpression.Create(memberExpr, valueParm);
...con:
Expression assignExpr = Expression.Assign(memberExpr, valueParm);
... e ottieni lo stesso risultato.