[英]How to assign properties generically using Expression.Assign selector with target type of object?
我正在嘗試使用表達式選擇器將屬性從一種對象類型普遍分配給另一種類型的屬性。 這是我到目前為止的代碼:
var type1 = new Type1();
var type2 = new Type2();
...
var propMap = new List<Tuple<Expression<Func<Type1, object>>, Expression<Func<TradeStaticAttributesItemModel, object>>>>
{
new Tuple<Expression<Func<Type1, object>>, Expression<Func<Type2, object>>>(x => x.Prop1, x => x.Prop1),
new Tuple<Expression<Func<Type1, object>>, Expression<Func<Type2, object>>>(x => x.Prop2, x => x.Prop2)
};
foreach (var prop in propMap)
{
if (prop.Item1.Compile()(type1) != prop.Item2.Compile()(type2))
{
ParameterExpression valueParameterExpression = Expression.Parameter(prop.Item2.Body.Type);
var assign = Expression.Lambda<Action<Type1, object>>(Expression.Assign(prop.Item1.Body, valueParameterExpression), prop.Item1.Parameters.Single(), valueParameterExpression);
Action<Type1, object> setter = assign.Compile();
setter(type1, prop.Item2.Compile()(type2));
}
}
但是,我是,但是,當屬性的類型為string
時,出現錯誤“類型為'System.String'的ParameterExpression無法用於類型為'System.Object'的委托參數” 。 我想除了object
以外的任何其他類型也會發生這種情況。 任何想法如何使這種代碼適用於這種情況?
我在這里使用Expression.Convert找到了一個可能的答案,但無法使其正常工作。
好的,我使用以下代碼進行了處理:
var type1 = new Type1();
var type2 = new Type2();
...
var propMap = new List<Tuple<Expression<Func<Type1, object>>, Expression<Func<TradeStaticAttributesItemModel, object>>>>
{
new Tuple<Expression<Func<Type1, object>>, Expression<Func<Type2, object>>>(x => x.Prop1, x => x.Prop1),
new Tuple<Expression<Func<Type1, object>>, Expression<Func<Type2, object>>>(x => x.Prop2, x => x.Prop2)
};
foreach (var prop in propMap)
{
if (prop.Item1.Compile()(type1) != prop.Item2.Compile()(type2))
{
ParameterExpression valueParameterExpression = Expression.Parameter(typeof(object));
// This handles nullable types
Expression targetExpression = prop.Item1.Body is UnaryExpression ? ((UnaryExpression)prop.Item1.Body).Operand : prop.Item1.Body;
var assign = Expression.Lambda<Action<Type1, object>>(
Expression.Assign(targetExpression, Expression.Convert(valueParameterExpression, targetExpression.Type)),
prop.Item1.Parameters.Single(),
valueParameterExpression);
Action<Type1, object> setter = assign.Compile();
setter(type1, prop.Item2.Compile()(type2));
}
}
這適用於可空類型(如注釋)。 我現在知道Expression,但是如果有人對此代碼有任何改進,請隨時提出建議!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.