与代表的反思

发布于 2025-02-13 17:08:26 字数 1008 浏览 2 评论 0原文

我正在使用反射比较两个对象列表(相同类型),并收集具有不同值的属性列表。我可能会通过大量对象列表进行比较,该对象列表以性能滞后结束。我开始知道在反思中使用代表,因此我们可以绕过需要时间的.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下伊人醉 2025-02-20 17:08:26

您可以通过将代码放入通用类中来缓存静态字段中的Getter委托。

然后,您可以使用更多仿制药使用两级类层次结构来缓存属性本身。

public List<string> Compare<T>(T A, T B)
{
    return GenericComparer<T>.Compare(A, B);
}

abstract class PropertyCompareBase<T>
{
    public string Name;
    public abstract bool CompareProperty(T A, T B);
}

class PropertyCompare<T, TProperty> : PropertyCompareBase<T>
{
    Func<T, TProperty> Getter;

    public PropertyCompare(PropertyInfo prop)
    {
        Name = prop.Name;
        Getter = (Func<T, TProperty>)prop.GetMethod.CreateDelegate(typeof(Func<T, TProperty>));
    }

    public override bool CompareProperty(T A, T B)
    {
        return EqualityComparer<TProperty>.Default.Equals(Getter(A), Getter(B));
    }
}

static class GenericComparer<T>
{
    static List<PropertyCompareBase<T>> properties = typeof(T)
        .GetProperties(BindingFlags.Public | BindingFlags.Instance)
        .Where(p => p.GetIndexParameters().Length == 0)
        .Select(p => (PropertyCompareBase<T>)Activator.CreateInstance(
                         typeof(PropertyCompare<,>)
                         .MakeGenericType(typeof(T), p.PropertyType),
                        p))
        .ToList();

    static public List<string> Compare(T A, T B)
    {
        List<string> unequalProperties =
            properties.Where(p => !p.CompareProperty(A, B))
                      .Select(p => p.Name)
                      .ToList();
        return unequalProperties;
    }
}

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.

public List<string> Compare<T>(T A, T B)
{
    return GenericComparer<T>.Compare(A, B);
}

abstract class PropertyCompareBase<T>
{
    public string Name;
    public abstract bool CompareProperty(T A, T B);
}

class PropertyCompare<T, TProperty> : PropertyCompareBase<T>
{
    Func<T, TProperty> Getter;

    public PropertyCompare(PropertyInfo prop)
    {
        Name = prop.Name;
        Getter = (Func<T, TProperty>)prop.GetMethod.CreateDelegate(typeof(Func<T, TProperty>));
    }

    public override bool CompareProperty(T A, T B)
    {
        return EqualityComparer<TProperty>.Default.Equals(Getter(A), Getter(B));
    }
}

static class GenericComparer<T>
{
    static List<PropertyCompareBase<T>> properties = typeof(T)
        .GetProperties(BindingFlags.Public | BindingFlags.Instance)
        .Where(p => p.GetIndexParameters().Length == 0)
        .Select(p => (PropertyCompareBase<T>)Activator.CreateInstance(
                         typeof(PropertyCompare<,>)
                         .MakeGenericType(typeof(T), p.PropertyType),
                        p))
        .ToList();

    static public List<string> Compare(T A, T B)
    {
        List<string> unequalProperties =
            properties.Where(p => !p.CompareProperty(A, B))
                      .Select(p => p.Name)
                      .ToList();
        return unequalProperties;
    }
}

dotnetfiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文