尝试编译以下代码时
IEquatable<Object> value = "test";
它导致此错误:
Cannot implicitly convert type 'string' to 'System.IEquatable<object>'
但是字符串实现了IEquatable<String>
MyCode:
public class Equals : Validation
{
public IEquatable<Object> Value { get; set; }
public Equals(IEquatable<Object> value)
{
Value = value;
}
public override bool Validate(Object value)
{
return Value.Equals(value);
}
}
用法
[Equals("test")]
public string Name { get; set; }
答案 0 :(得分:0)
字符串实现IEquatable<String>
而不是IEquatable<Object>
因此IEquatable<String> value = "test"
应该可以工作
答案 1 :(得分:0)
为了使代码正常工作,您不能将字符串的值插入对象中以便使用:
IEquatable<string> value ="test";