本文介绍了C# - 字典 - 文件路径(Custom EqualityComparer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:自定义对象实现了EqualityComparer和IEquatable,但Dictionary并不总是使用这些方法。



上下文: / strong>我已经创建了一个助手类, FilePath 来处理文件路径,而不是将它们视为字符串。辅助类负责确定两个文件路径是否相等。然后,我需要将FilePaths存储在 Dictionary< FilePath,object> 中。



目标: strong>

  Assert.True(new FilePath(@c:\temp\file.txt)。Equals 
new FilePath(@C:\TeMp\FIle.tXt));

var dict = new Dictionary< FilePath,object>
{
{new FilePath(@c:\temp\file.txt),new object()}
}

Assert.True(dict.ContainsKey(new FilePath(@ c:\temp\file.txt));

Assert.True(dict.ContainsKey(new FilePath(@C:\TeMp\FIle.tXt));

我创建了我的FilePath类:
public class FilePath:EqualityComparer,IEquatable
{
private string _fullPath;

  public FilePath(string fullPath)
{
_fullPath = Path.GetFullPath(fullPath);
}

public override bool Equals(FilePath x,FilePath y)
{
if(null == x || null == y)
return false;

return(x._fullPath.Equals(y._fullPath,StringComparison.InvariantCultureIgnoreCase));
}

public override int GetHashCode(FilePath obj)
{
return obj._fullPath.GetHashCode();
}

public bool Equals(FilePath other)
{
return Equals(this,other);
}

public override bool Equals(object obj)
{
return Equals(this,obj as FilePathSimple);
}
}

问题: p>

断言1和2通过,但Assert 3失败:

 断言。 True(dict.ContainsKey(new FilePath(@C:\TeMp\FIle.tXt)); 


解决方案

我还需要覆盖GetHashCode():

  int GetHashCode()
{
return _fullPath.ToLower()。GetHashCode();
}

参考文献:




Problem: Custom Object implements EqualityComparer and IEquatable, but Dictionary doesn't always use these methods.

Context: I have created a helper class, FilePath for dealing with file paths instead of treating them as strings. The helper class is responsible for determining if two file paths are equal. I then need to store FilePaths in a Dictionary<FilePath, object>.

Goal:

  Assert.True(new FilePath(@"c:\temp\file.txt").Equals(
      new FilePath(@"C:\TeMp\FIle.tXt")); 

  var dict = new Dictionary<FilePath, object>
  {
      {new FilePath(@"c:\temp\file.txt"), new object()}
  }

  Assert.True(dict.ContainsKey(new FilePath(@"c:\temp\file.txt"));

  Assert.True(dict.ContainsKey(new FilePath(@"C:\TeMp\FIle.tXt"));

I've created my FilePath class: public class FilePath : EqualityComparer, IEquatable { private string _fullPath;

    public FilePath (string fullPath)
    {
        _fullPath = Path.GetFullPath(fullPath);
    }

    public override bool Equals(FilePath x, FilePath y)
    {
        if (null == x || null == y)
            return false;

        return (x._fullPath.Equals(y._fullPath, StringComparison.InvariantCultureIgnoreCase));
    }

    public override int GetHashCode(FilePath obj)
    {
        return obj._fullPath.GetHashCode();
    }

    public bool Equals(FilePath other)
    {
        return Equals(this, other);
    }

    public override bool Equals(object obj)
    {
        return Equals(this, obj as FilePathSimple);
    }
}

Question:

Assert 1 and 2 pass, but Assert 3 fails:

 Assert.True(dict.ContainsKey(new FilePath(@"C:\TeMp\FIle.tXt"));
解决方案

I needed to override GetHashCode() as well:

 public override int GetHashCode()
 {
     return _fullPath.ToLower().GetHashCode();
 }

References: What's the role of GetHashCode in the IEqualityComparer<T> in .NET?

Compiler Warning (level 3) CS0659

这篇关于C# - 字典 - 文件路径(Custom EqualityComparer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 04:14