我想有这种设计的:类具有相同的接口,但不同类型的属性
public interface IDifferentTypes
{
}
public class IntegerType : IDifferentTypes
{
public int value { get; set; }
}
public class StringType : IDifferentTypes
{
public string value { get; set; }
}
public class DateTimeType : IDifferentTypes
{
public DateTime value { get; set; }
}
但在接口中定义的属性“值”。
所以我可以调用类似的东西:
IDifferentTypes someInt = GetSomeInt(); // GetSomeInt() returns a IntegerType object
Assert.AreEqual(5, someInt.value);
IDifferentTypes someString = GetSomeString(); // GetSomeString() returns a StringType object
Assert.AreEqual("ok", someString.value);
问题是,值的类型是每个实施不同的,什么是应对的最佳途径?
接口无法指定字段;它将需要一个属性 – Servy 2013-04-05 14:43:11
不幸的是,你还必须在代码中指定封闭的泛型类型,所以你不能概括一个字符串和一个int之间的接口 - 而不是没有另一个接口可能暴露'对象'价值。 – 2013-04-05 14:44:37
但是我们必须在设计时知道需要返回哪种类型? – ibiza 2013-04-05 14:44:49