![](/img/trans.png)
[英]Is it possible to find a given child class in a list of parent classes in C#?
[英]C# find child class via parent class
我有 3 個類PropiedadEspecificaFilm
、 PropiedadEspecificaBook
和PropiedadEspecificaMusic
繼承自Product
。
public partial class PropiedadEspecificaBook : Product
{
public string ISBN { get; set; }
public string author { get; set; }
public long productId { get; set; }
//Equals
//ToString
}
public partial class PropiedadEspecificaMusic : Product
{
public string name { get; set; }
public string artist{ get; set; }
public long productId { get; set; }
//Equals
//ToString
}
public partial class PropiedadEspecificaMusic : Product
{
public string name { get; set; }
public string artist{ get; set; }
public long productId { get; set; }
//Equals
//ToString
}
public partial class Product
{
public Product()
{
this.OrderLines = new HashSet<OrderLine>();
this.Comments = new HashSet<Comment>();
}
public long id { get; set; }
public string productName { get; set; }
public decimal price { get; set; }
public System.DateTime releaseDate { get; set; }
public short stock { get; set; }
public long categoryId { get; set; }
}
有沒有辦法知道PropiedadEspecifica
中的哪一個具有我正在尋找的productId
?
是的,如果所有產品共享一個屬性,您應該在父 class 而不是在子中聲明。 子級應僅具有與父級 class 不同的屬性。
我也強烈懷疑您不需要將您的類標記為部分(如果您想拆分 class 定義,則使用它,它不用於繼承)。
我也進行了更改,因此公共屬性大寫,因為這是 c# 命名的一般約定。
public class PropiedadEspecificaBook : Product
{
public string ISBN { get; set; }
public string Author { get; set; }
}
public class PropiedadEspecificaMusic : Product
{
public string Name { get; set; }
public string Artist{ get; set; }
}
public class PropiedadEspecificaMusic : Product
{
public string Name { get; set; }
public string Artist{ get; set; }
}
public class Product
{
public Product()
{
this.OrderLines = new HashSet<OrderLine>();
this.Comments = new HashSet<Comment>();
}
public long Id { get; set; }
public string ProductName { get; set; }
public decimal Price { get; set; }
public System.DateTime ReleaseDate { get; set; }
public short Stock { get; set; }
public long CategoryId { get; set; }
public long ProductId { get; set; }
}
我通過在 Product 中添加一個虛擬 Dictionary<String, String> FindPropiedadEspecificaMethod() 解決了這個問題。 然后在每個 PropiedadEspecifica 中覆蓋。 對不起,因為問題解釋得不好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.