我有一个类:
public class Person
{
public string name { get; set; }
public int age { get; set; }
}
Person p1 = new Person() { age=1, name="a" };
Person p2 = new Person() { age = 1, name = "b" };
我想做类似的事情
p1.equals(p2)
但按年龄或按名字
我的意思不是添加到字典的方式(并使用 Iequatable...)
Equals 方法内部没有任何 Helper ...
有什么办法可以让他发送我的特定助手类,就像:
Dictionary<PersonForDictionary, int> dct2 =
new Dictionary<PersonForDictionary, int>(new helperClass()); // here helperclass is a class which I gave him - and told the dictionary how to equate objects....
我正在寻找一个类似 的解决方案来等同对象。 (不是字典模式)。
可能是,也可能是对 IComparable
进行不同的实现。例如:
public class Person
{
public string name { get; set; }
public int age { get; set; }
pulbic int ComparePerson(Person person, IComparable comparer)
{
return comparer.Compare(this, person);
}
}
实现不同类后:
public class PersonNameComparer : IComparer
{
}
或另一个
public class PersonAgeComparer : IComparer
{
}
并在内部使用它:
Person p1 = new Person() { age=1, name="a" };
Person p2 = new Person() { age = 1, name = "b" };
p1.Compare(p2, new PersonNameComparer ());
p1.Compare(p2, new PersonAgeComparer ());
我是一名优秀的程序员,十分优秀!