使用以下函数为通用列表的每个项创建相似度:
private static IEnumerable<Likeness<T, T>> CreateLikenessOfListItems<T>(
IEnumerable<T> list)
where T : class
{
var likeness = from item in list
select item.AsSource()
.OfLikeness<T>();
return likeness;
}
...然后可以比较两个列表:
private static void AssertExpectedAndActualListsHaveSameElements<T>(
IEnumerable<T> actual,
IEnumerable<Likeness<T, T>> expected)
where T : class
{
IEnumerable<Object> expectedAsObjects = expected;
IEnumerable<Object> actualAsObjects = actual;
bool sequencesEqual = expectedAsObjects.SequenceEqual(actualAsObjects);
Assert.IsTrue(sequencesEqual, "Expected and actual lists elements differ.");
}
问题:是否可以动态地为未知数量的属性添加Witout()? 像WitoutAll()这样的东西会:
private static IEnumerable<Likeness<T, T>> CreateLikenessOfListItemsWithou<T, TProperty>(
IEnumerable<T> list,
IEnumerable<Expression<Func<T, TProperty>>> propertyPickers)
where T : class
where TProperty : class
{
var likeness = from item in list
select item.AsSource()
.OfLikeness<T>()
.WithoutAll(propertyPickers); // <= for all pickers
return likeness;
}