private Equipment GenerateDirtyPropertiesOnEntity(Equipment updatedEntity)
{
updatedEntity.DirtyProperties.Clear();
Equipment originalEntity = GetEquipmentByGuid(updatedEnitity.Guid.Value);
Type myType = updatedEntity.GetType();
System.Reflection.PropertyInfo[] properties = myType.GetProperties();
foreach (System.Reflection.PropertyInfo p in properties)
{
if (p.GetValue(originalEntity, null) == null)
{
if (p.GetValue(updatedEntity, null) != null)
updatedEntity.DirtyProperties.Add(p.Name);
}
else
{
if (!(p.GetValue(originalEntity, null).Equals(p.GetValue(updatedEntity, null))))
updatedEntity.DirtyProperties.Add(p.Name);
}
}
return updatedEntity;
}
使用它时我牺牲了多少速度?
有谁知道更好的方法来做到这一点?
提前致谢
最佳答案
您 可能 通过尝试使用 INotifyPropertyChanged 接口(interface)来获得更好的性能。您可以使用基于事件的建模来完成相同的事情,而不是使用反射。
CSLA.NET 是采用这种方法的框架示例。
示例
T SomeProperty()
{
get
{
return _someProperty;
}
set
{
if (_someProperty <> value)
{
_someProperty = value;
OnPropertyChanged("SomeProperty");
}
}
}
然后 OnPropertyChanged 看起来像
OnPropertyChanged(object params)
{
DirtyProperties.Add(params);
}
请记住,这是总空气代码。我不记得 params 是如何构造的,但它实际上不是 object 类型,并且包含了属性的名称,这是您确定将哪个属性添加到 DirtyProperties 列表的方式。
关于c# - 反射相关 我牺牲了多少速度?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/766331/