【问题标题】:Losing sub classes' properties in a collection of inherited classes在继承类的集合中丢失子类的属性
【发布时间】:2013-07-18 13:33:12
【问题描述】:

我正在尝试实现一个相对简单的类层次结构。但是,我的一些知识似乎从学校开始就丢失了。问题是子类的属性在检索时丢失了。

层次结构如下:

  • 具有子类一般属性的抽象类
  • 具有特定属性的子类。从抽象类继承
  • 子类的集合

收藏

public class ActiveFilters
{
    public List<ActiveFilter> Filters { get; set; }
    enum FilterTypes 
    { 
        DateRange = 1,
        CheckBox = 2,
        TextBox = 3,
        RadioButtons = 4
    };

    public ActiveFilters()
    {
        Filters = new List<ActiveFilter>();

        //Required filters on start
        Filters.Add(
            new FilterDateRange(
                "/statistikstart.aspx",
                (int)FilterTypes.DateRange,
                "paymentDate",
                new DateTime(1800, 1, 1),
                DateTime.Now.AddDays(7)
            )
        );
        Filters.Add(
            new FilterDateRange(
                "/statistikgiro.aspx",
                (int)FilterTypes.DateRange,
                "paymentDate",
                new DateTime(1800, 1, 1),
                DateTime.Now
            )
        );
    }

    public ActiveFilter GetActiveFilter(string source, string name)
    {
        try
        {
            return Filters.FirstOrDefault(x => x.Source == source && x.Name == name) as ActiveFilter;
        }
        catch
        {
            //Filter not in collection
            return null;
        }
    }
}

抽象类

public abstract class ActiveFilter
{
    public string Source { get; set; }
    public int Type { get; set; }
    public string Name { get; set; }
}

子类

public class FilterDateRange : ActiveFilter
{
    public DateTime FromDate { get; set; }
    public DateTime ToDate { get; set; }

    public FilterDateRange(string source, int type, string name, DateTime fromDate, DateTime toDate)
    {
        base.Source = source;
        base.Type = type;
        base.Name = name;
        this.FromDate = FromDate;
        this.ToDate = ToDate;
    }
}

当我尝试从集合构造函数中检索创建的过滤器之一时,没有从子类中保存日期。唯一保存的信息是抽象类的属性:

FilterDateRange test = ((FilterDateRange)((ActiveFilters)Session["activeFilters"]).GetActiveFilter("/statistikstart.aspx", "paymentDate"));

问题是我从 GetActiveFilter() 而不是子类返回 ActiveFilter 吗?如果是这样,当过滤器可以属于不同的子类时,如何重写 GetActiveFilter() 以返回过滤器?

【问题讨论】:

    标签: c# asp.net oop


    【解决方案1】:

    你有一些错别字:

    public FilterDateRange(string source, int type, string name, DateTime fromDate, DateTime toDate)
    {
        base.Source = source;
        base.Type = type;
        base.Name = name;
        this.FromDate = FromDate; // set FromDate to FromDate (the property) 
        this.ToDate = ToDate;
    }
    

    应该是

    public FilterDateRange(string source, int type, string name, DateTime fromDate, DateTime toDate)
    {
        base.Source = source;
        base.Type = type;
        base.Name = name;
        this.FromDate = fromDate; // set FromDate to fromDate (the parameter)
        this.ToDate = toDate;
    }
    

    【讨论】:

    • 哇,现在我觉得自己很傻......反正我自己可能不会看到它,所以谢谢你!
    猜你喜欢
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 1970-01-01
    相关资源
    最近更新 更多