如何将ICollection <subclass>转换为ICollection <baseclass>?

时间:2016-03-04 11:18:13

标签: c#

我有这个模型结构:

[Table("Image")]
public abstract class CBaseImage
{

    public int ID { get; set; }


    [Required]
    public string Extension { get; set; }

    [MaxLength(11)]
    [Required]
    public string OriginalUrl { get; set; }

    [MaxLength(11)]
    [Required]
    public string ThumbUrl { get; set; }

    public int OrderNumber { get; set; }
}




public class CNewBuildingLayoutImage : CBaseImage
{
    public int NewBuildingLayoutID { get; set; }
    public virtual CNewBuildingLayout NewBuildingLayout { get; set; }


    [AImageFormat(527, 584, false)]
    [MaxLength(11)]
    [Required]
    public string WH_527_584_Url { get; set; }


    [AImageFormat(304, 342, false)]
    [MaxLength(11)]
    [Required]
    public string WH_304_342_Url { get; set; }



}

[Table("NewBuildingLayout")]

public class CNewBuildingLayout
{

    public int ID { get; set; }
    //.........

    public virtual ICollection<CNewBuildingLayoutImage> Images { get; set; }

}

在某些时候,我需要使用CNewBuildingLayout.Images作为ICollection<CNewBuildingLayoutImage> AS ICollection<CBaseImage>的{​​{1}}, 但是当我这样做时:

(ICollection<CBaseImage>)someLayout.Images

我明白了:

  

无法投射类型的对象   'System.Collections.Generic.HashSet'1 [Models.CNewBuildingLayoutImage]'   输入   'System.Collections.Generic.HashSet'1 [Models.CBaseImage]'。

我试过

  (ICollection<CBaseImage>)someLayout.Images.Cast<CBaseImage>()

同样的结果。

如何将ICollection<Subclass>投射到ICollection<BaseClass>

0 个答案:

没有答案