我有一个这样的类。带隐式类型转换的调用函数
public class ForeignKey {
public string Id {get;}
public TableA TableA {get;}
public TableB TableB {get;}
public static implicit operator string(ForeignKey obj){ return obj.Id; }
public override string ToString() { return Id; }
/* various operator overloads */
}
我想有它的自动类型转换,所以我可以使用它像一个字符串。到目前为止,我所做的事情让我可以在没有明确表演的情况下在很多地方使用它。但是,我无法找到一种方法来调用字符串函数,而无需进行明确的转换。
例如,我想让这个工作。
if (Key.EndsWith(someValue))
目前,我必须这样做
if (((string)Key).EndsWith(someValue))
// or
if (Key.Id.EndsWith(someValue))
有没有办法得到它的工作,我想如何?
感谢
Key.Id已经是字符串。您的最后一个选项Key.Id.EndsWith(someValue)应该会自动工作。 – Amit
您可以使用签名创建一个C#扩展方法public static KeyValue(this Key key){} – Amit
Right,Key.Id.EndsWith可以工作,但我希望获得Key.EndsWith的工作。 –