gpt4 book ai didi

c# - 自动比较属性

转载 作者:太空狗 更新时间:2023-10-29 17:46:34 26 4
gpt4 key购买 nike

我想获取为匹配对象而更改的所有属性的名称。我有这些(简化的)类:

public enum PersonType { Student, Professor, Employee }

class Person {
public string Name { get; set; }
public PersonType Type { get; set; }
}

class Student : Person {
public string MatriculationNumber { get; set; }
}

class Subject {
public string Name { get; set; }
public int WeeklyHours { get; set; }
}

class Professor : Person {
public List<Subject> Subjects { get; set; }
}

现在我想获取属性值不同的对象:

List<Person> oldPersonList = ...
List<Person> newPersonList = ...
List<Difference> = GetDifferences(oldPersonList, newPersonList);

public List<Difference> GetDifferences(List<Person> oldP, List<Person> newP) {
//how to check the properties without casting and checking
//for each type and individual property??
//can this be done with Reflection even in Lists??
}

最后,我想要一个这样的差异列表:

class Difference {
public List<string> ChangedProperties { get; set; }
public Person NewPerson { get; set; }
public Person OldPerson { get; set; }
}

ChangedProperties 应包含已更改属性的名称。

最佳答案

我花了很长时间尝试使用类型化委托(delegate)编写一个更快的基于反射的解决方案。但最终我放弃了,转而使用Marc Gravell'sFast-Member library获得比普通反射更高的性能。

代码:

internal class PropertyComparer
{
public static IEnumerable<Difference<T>> GetDifferences<T>(PropertyComparer pc,
IEnumerable<T> oldPersons,
IEnumerable<T> newPersons)
where T : Person
{
Dictionary<string, T> newPersonMap = newPersons.ToDictionary(p => p.Name, p => p);
foreach (T op in oldPersons)
{
// match items from the two lists by the 'Name' property
if (newPersonMap.ContainsKey(op.Name))
{
T np = newPersonMap[op.Name];
Difference<T> diff = pc.SearchDifferences(op, np);
if (diff != null)
{
yield return diff;
}
}
}
}

private Difference<T> SearchDifferences<T>(T obj1, T obj2)
{
CacheObject(obj1);
CacheObject(obj2);
return SimpleSearch(obj1, obj2);
}

private Difference<T> SimpleSearch<T>(T obj1, T obj2)
{
Difference<T> diff = new Difference<T>
{
ChangedProperties = new List<string>(),
OldPerson = obj1,
NewPerson = obj2
};
ObjectAccessor obj1Getter = ObjectAccessor.Create(obj1);
ObjectAccessor obj2Getter = ObjectAccessor.Create(obj2);
var propertyList = _propertyCache[obj1.GetType()];
// find the common properties if types differ
if (obj1.GetType() != obj2.GetType())
{
propertyList = propertyList.Intersect(_propertyCache[obj2.GetType()]).ToList();
}
foreach (string propName in propertyList)
{
// fetch the property value via the ObjectAccessor
if (!obj1Getter[propName].Equals(obj2Getter[propName]))
{
diff.ChangedProperties.Add(propName);
}
}
return diff.ChangedProperties.Count > 0 ? diff : null;
}

// cache for the expensive reflections calls
private Dictionary<Type, List<string>> _propertyCache = new Dictionary<Type, List<string>>();
private void CacheObject<T>(T obj)
{
if (!_propertyCache.ContainsKey(obj.GetType()))
{
_propertyCache[obj.GetType()] = new List<string>();
_propertyCache[obj.GetType()].AddRange(obj.GetType().GetProperties().Select(pi => pi.Name));
}
}
}

用法:

PropertyComparer pc = new PropertyComparer();
var diffs = PropertyComparer.GetDifferences(pc, oldPersonList, newPersonList).ToList();

性能:

我非常有偏见的测量表明,这种方法比 Json 转换快 4-6 倍,比普通反射快约 9 倍。但公平地说,您可能会大大加快其他解决方案的速度。

限制:

目前,我的解决方案不会对嵌套列表进行递归,例如,它不会比较单个 Subject 项目 - 它只会检测到主题列表不同,但不会检测内容或位置。但是,在需要时添加此功能应该不会太难。最困难的部分可能是决定如何在 Difference 类中表示这些差异。

关于c# - 自动比较属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17155870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com