与代表的反思
我正在使用反射比较两个对象列表(相同类型),并收集具有不同值的属性列表。我可能会通过大量对象列表进行比较,该对象列表以性能滞后结束。我开始知道在反思中使用代表,因此我们可以绕过需要时间的.getValue(obj,null)
。我的班级属性的类型是广泛的。它可以是字符串,int和枚举。可用的解决方案对我没有解决。感谢您的帮助。
我正在使用的代码比较同一类型的两个对象,
public List<string> Compare<T>(object A, object B)
{
var type = typeof(T);
var allProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
List<string> unequalProperties =
(
from pi in allProperties
let AValue = type.GetProperty(pi.Name).GetValue(A, null)
let BValue = type.GetProperty(pi.Name).GetValue(B, null)
where AValue != BValue && (AValue == null || !AValue.Equals(BValue))
select pi.Name
).ToList();
return unequalProperties;
}
两个.getValue(a/b,null)
正在生成我想使用委托人跳过的滞后。
I'm using reflection to compare two list of objects(of same type) and collecting the list of properties which have different values. I may iterate through lots of object list to compare which ended in performance lag. I came to know about using delegates in the reflection so we can bypass the .GetValue(obj, null)
which is taking time. The types of my class properties is wide. It can be string, int and enum. The available solutions are not working out for me. Thanks for your help.
The code I'm using to compare the 2 objects of same type
public List<string> Compare<T>(object A, object B)
{
var type = typeof(T);
var allProperties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
List<string> unequalProperties =
(
from pi in allProperties
let AValue = type.GetProperty(pi.Name).GetValue(A, null)
let BValue = type.GetProperty(pi.Name).GetValue(B, null)
where AValue != BValue && (AValue == null || !AValue.Equals(BValue))
select pi.Name
).ToList();
return unequalProperties;
}
The two .GetValue(A/B, null)
is producing the lag which i want to skip using delegates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过将代码放入通用类中来缓存静态字段中的Getter委托。
然后,您可以使用更多仿制药使用两级类层次结构来缓存属性本身。
dotnetfiddle
You can cache the getter delegates in a static field, by putting your code into a generic class.
You can then cache the properties themselves using a two-level class hierarchy using some more generics.
dotnetfiddle