jeudi 7 juillet 2022

Reflection with Delegates

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 lakhs 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.





Aucun commentaire:

Enregistrer un commentaire