我有SortedDictionary<Package, List<string>>
。以下是Package类:
using System;
namespace GetPackageInfo
{
public class Package : IComparable, IEquatable<Package>
{
public string PackageName;
public string Version;
public Package()
{
}
public Package(string packageName)
{
this.PackageName = packageName;
}
public override int GetHashCode()
{
unchecked
{
int result = 17;
result = result * 23 + ((PackageName != null) ? this.PackageName.GetHashCode() : 0);
result = result * 23 + ((Version != null) ? this.Version.GetHashCode() : 0);
return result;
}
}
public bool Equals(Package other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return Equals(this.PackageName, other.PackageName) &&
Equals(this.Version, other.Version);
}
public override bool Equals(object obj)
{
Package temp = obj as Package;
if (temp == null)
return false;
return this.Equals(temp);
}
public override string ToString()
{
return string.Format("PackageName: {0}, Version: {1}", PackageName, Version);
}
public int CompareTo(object obj)
{
if (obj == null)
return 1;
if (obj != null)
return (Equals(obj) ? -1 : 1);
else
throw new ArgumentException("Object is not a Temperature");
}
每当我在Contains
上执行ContainsKey
或SortedDictionary
时,即使名称和版本相同,它也不起作用。
if (nugetPackagesInfo.Keys.Any(p => p.Equals(package)))
{
//List<string> currPackage;
//nugetPackagesInfo.TryGetValue(package, out currPackage);
if (!nugetPackagesInfo[package].Contains(packageFile))
{
nugetPackagesInfo[package].Add(packageFile);
}
nuGetPackagesInfo
是我的字典对象。但Any
返回true。但是一旦它被传递到nugetPackagesInfo[package]
,它就会抛出KeyNotFoundException
。能帮我解决一下吗?我的CompareTo
不正确吗?
答案 0 :(得分:2)
您CompareTo
的实施似乎并不正确。实际上,您没有实现任何订购的软件包。您应该,最有可能按名称订购包,如果版本相同,则应该。
Package.CompareTo
的核心应该是这样的(简化;不处理other == null
):
// try name ordering
int nameOrdering = this.Name.CompareTo(other.Name);
// names not equal ⇒ ordering is clear and no need to inspect further
if (nameOrdering != 0)
{
return nameOrdering;
}
// names are equal ⇒ resort to version ordering
else
{
return this.Version.CompareTo(other.Version);
}
你也应该read the documentation for String.CompareTo()
,因为它具有特定于文化的语义。
答案 1 :(得分:1)
您的CompareTo
方法应该如下:
-1
更小而不是this
obj
1
更大而不是this
并且obj
0
等于 this
obj
答案 2 :(得分:1)
将CompareTo
和GetHashCode
更改为这些实施。
public int CompareTo(object obj)
{
if (obj == null)
return 1;
return this.ToString().CompareTo(obj.ToString());
}
public override int GetHashCode()
{
unchecked
{
return ((PackageName != null ? PackageName.GetHashCode() : 0)*397) ^ (Version != null ? Version.GetHashCode() : 0);
}
}
public override string ToString()
{
return string.Format("PackageName: {0}, Version: {1}", PackageName??"", Version ?? "");
}
CompareTo
- 请参阅documentation。通过使用ToString(),您可以在包名称和版本上进行比较,而无需进行自己的检查。只要确保ToString是正确的(我添加了null检查,因此它不会抛出异常)。
GetHashCode
- 不确定从哪里获得实施。您需要确保哈希码对于该项始终是唯一的,除非它们确实相等。这是我found on this previous answer on SO的实现,请参阅答案中的最后一个编辑..