我想使用 List[MyObject] 比较一个属性而不是整个对象。因此,我使用 IEquatable[MyObject] 但编译器仍然需要 MyObject 而不是字符串属性。为什么?
这是我得到的:
public class AnyClass
{
public List<AnyOtherClass> MyProperty { get; set; }
public string AnyProperty { get; set; }
public AnyClass(string[] Names, string[] Values, string AnyProperty)
{
this.AnyProperty = AnyProperty;
this.MyProperty = new List<AnyOtherClass>();
for (int i = 0; i < Names.Length; i++)
MyProperty.Add(new AnyOtherClass(Names[i], Values[i]));
}
}
public class AnyOtherClass : IEquatable<string>
{
public AnyOtherClass(string Name, string Values)
{
this.Name = Name;
this.Values = Values.Split(';').ToList();
}
public string Name { get; set; }
public List<string> Values { get; set; }
public bool Equals(string other)
{
return this.Name.Equals(other);
}
}
private void DoSomething()
{
string[] Names = new string[] { "Name1", "Name2" };
string[] Values = new string[] { "Value1_1;Value1_2", "Value2" };
AnyClass ac = new AnyClass(Names, Values, "any Property");
if (ac.MyProperty.Contains("Name1")) //Problem is here...
//do something
}