Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通用IEqualityComparer使用反射和属性标记要比较的内容_C# - Fatal编程技术网

C# 通用IEqualityComparer使用反射和属性标记要比较的内容

C# 通用IEqualityComparer使用反射和属性标记要比较的内容,c#,C#,Intersect应已生成0条记录,因为firstname和lastname不匹配。 我哪里出错了。。。。?我怀疑是在执行中 谢谢你所有的问题都解决了(特别感谢Lasse V.Karlsen用gethashcode的钱):等于加上hashcode错误修复了问题。 测试代码 根据我的高中数学: SetA = {test1, test2} SetB = {test1, test2} SetA = SetB (conceptually speaking) => SetA (intersect)

Intersect应已生成0条记录,因为firstname和lastname不匹配。 我哪里出错了。。。。?我怀疑是在执行中

谢谢你所有的问题都解决了(特别感谢Lasse V.Karlsen用gethashcode的钱):等于加上hashcode错误修复了问题。 测试代码
根据我的高中数学:

SetA = {test1, test2}
SetB = {test1, test2}
SetA = SetB (conceptually speaking) => SetA (intersect) SetB = SetA
或者你期望看到什么

如果你把它改成

SetA = {test1}
SetB = {test2}
您将获得
{0}
(空集)

无论如何,我不建议将此用于prod代码。至少,当您已经检查了ReflectionHelper中的类时,可以缓存属性信息


如果您有很多比较,那么这段代码的运行速度(相对而言)会非常慢(正如您在实现这段代码的推理中所暗示的)。虽然我非常喜欢避免编写锅炉板代码,但有时出于性能/可维护性的原因,编写它是值得的。

在任何情况下,即使比较器工作正常,他也会将相同的两个实例放在两个列表中,我不明白他在这里测试什么。另外,他为他的场景实现了一个非常不正确的GetHashCode,它应该使用与比较器计算hashcode相同的属性。实际上,它们应该是hashcode的相同属性。我是说你的实现是错误的。您可以读取propertyinfo对象,这些对象将始终保持不变。它应该基于这些属性的值。请不要更新您的问题以包含答案。如果您回答了自己的问题,请将您的解决方案作为答案发布并接受。这就是我得到结果=2的点。我的目标是按照高中数学的要求得到0。使用except而不是intersect;-)var results=listA.Except(listB,new Compare(ListClassA)).ToList();可能是拉塞夫。卡尔森说的==。我需要替换对==的任何调用。。。我认为…问题在于intersect背后的数学。如果将==替换为.equals,则不会有任何区别。在你的例子中,string.equals和==是一样的,这是因为你在这里很幸运(尽管你应该用.equals来替换它,以确保它也适用于复杂类型)。Intersect意味着给定一个子集a{1 2 3}Intersect b{1}结果集是c{1}或者我疯了。我已经做了具体的实现,这是可行的,但我在一般的一个失败。
public class ReflectionHelper 
        {
            public IList<string> GetPropertyNames<T>() 
            {
                IList<string> propertyNames = new List<string>(); 
                var propertyInfos = typeof(T).GetProperties(
                    BindingFlags.Public |
                    BindingFlags.Static |
                    BindingFlags.NonPublic |
                    BindingFlags.Default |
                    BindingFlags.Instance).ToList().
                    Where(prop => Attribute.IsDefined(prop, typeof(KeyComparerAttribute)));
                // write property names
                if (propertyInfos.ToList().Count > 0)
                {
                    foreach (PropertyInfo propertyInfo in propertyInfos)
                    {
                       propertyNames.Add(propertyInfo.Name);
                    }
                }
                return propertyNames;
            }
            public string GetPropertyValue<T>(string propName, T src)
            {
                return src.GetType().GetProperty(propName).GetValue(src, null).ToString();
            }
    }
 public class ClassA
    {

        public int employeeid { get; set; }
        [KeyComparer]
        public string firstName { get; set; }
        [KeyComparer]
        public string lastname { get; set; }
    }
SetA = {test1, test2}
SetB = {test1, test2}
SetA = SetB (conceptually speaking) => SetA (intersect) SetB = SetA
SetA = {test1}
SetB = {test2}