实现一个基本接口,其中定义产品的
Id
财产:
public interface IProduct
{
int Id { get; set; }
}
定义实际的类,并确保它们实现
IProduct
:
public class FruitProduct : IProduct
{
public int Id { get; set; }
public string Name { get; set; }
public string Variety { get; set; }
}
public class CerealProduct : IProduct
{
public int Id { get; set; }
public string Brand { get; set; }
public int SugarContent { get; set; }
}
最后的查找将指定它是
i-乘积
(这样,您就可以访问ID字段,该字段应该出现在
FruitProduct
或
CerealProduct
你发送到
ProductLookup
)
public class ProductLookup<T> where T : IProduct
{
public static T GetProduct(int id)
{
// var productType = GetProductType(id);
//You can pass either FruitProduct or CerealProduct here (Although, you will ONLY
//be able to access Id here, as this function only knows it's an IProduct)
return this.db<T>()
.Single(p => p.Id == id);
}
}