0
我有以下方法來比較DTO。使用表達式比較屬性和子屬性上的對象
bool Equals<T1, T2>(T1 t1, T2 t2, params Expression<Func<T1, object>>[] accessors)
{
return !(
from accessor in accessors
select ((MemberExpression) accessor.Body).Member.Name into propertyName
let p1 = typeof (T1).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
let p2 = typeof (T2).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
let p1val = p1.GetValue(t1, null)
let p2val = p2.GetValue(t2, null)
where !Equals(p1val, p2val)
select p1val
).Any();
}
我可以使用調用這個(a
和b
是對象的情況下,按照慣例共享相同的屬性,但不是相同的對象):
Equals(a, b, x => x.PropertyOne, x => x.PropertyTwo);
哪些對象進行比較財產財產,這在大多數情況下都很好。
但是,我發現一個案例,我需要比較具有複雜類型屬性的對象,以及我想要在複雜類型而不是對象上比較屬性的對象。事情是這樣的:
Equals(a, b, x => x.ComplexTypeProperty.ChildProp);
我已經意識到我需要離開舒適的反映比較和輸入表達式的土地,但這裏的主要任務是能夠表達兩個屬性訪問和屬性訪問通過複雜的類型屬性,這就是我迷失的地方。
任何指針都會很好,謝謝!