C# 如何通过继承扩展属性?

C# 如何通过继承扩展属性?,c#,inheritance,enums,properties,constraints,C#,Inheritance,Enums,Properties,Constraints,我有一个类产品,它包含一个带有价格曲线的字典。键是可以解析为时间戳的字符串 public class Product { public virtual int Id { get; set; } public virtual IDictionary<string, decimal> PriceList { get; set; } public Product() { this.PriceList = new Dictionary<

我有一个类产品,它包含一个带有价格曲线的字典。键是可以解析为时间戳的字符串

public class Product
{
    public virtual int Id { get; set; }
    public virtual IDictionary<string, decimal> PriceList { get; set; }

    public Product()
    {
        this.PriceList = new Dictionary<string, decimal>();
    }
}
公共类产品
{
公共虚拟整数Id{get;set;}
公共虚拟IDictionary价目表{get;set;}
公共产品()
{
this.PriceList=新字典();
}
}
现在我需要一个二等舱,每把钥匙都有更多的价格

public class SpecialProduct : Product
{
    public enum PriceType
    {
        BusineesDays,
        Weekends,
        Holidays
    }

    public virtual IDictionary<string, IDictionary<PriceType, decimal>> PriceList { get; set; }

    public SpecialProduct()
    {
        this.PriceList = new Dictionary<string, IDictionary<PriceType, decimal>>();
    }
}
公共类特殊产品:产品
{
公共枚举价格类型
{
繁忙的星期四,
周末,
假期
}
公共虚拟IDictionary价目表{get;set;}
公共专用产品()
{
this.PriceList=新字典();
}
}
我不确定这是否是一个很好的方法。我还想将枚举类型限制为decimal。有什么想法吗


更新:我忘了提到我需要将所有产品保存到一个通用列表(List)

我会简化,因为这段代码有某种不好的味道


为什么不在
SpecialProduct
中的
PriceList
中也使用
Product
中的
PriceList,并使子字典只有一个值始终为
PriceType。默认值
或类似的值?然后,您就可以使用所有
产品
,而不需要任何类型转换或检查。

我会简化,因为这段代码有某种难闻的味道


为什么不在
SpecialProduct
中的
PriceList
中也使用
Product
中的
PriceList,并使子字典只有一个值始终为
PriceType。默认值
或类似的值?这样,您就可以使用所有
产品
,而无需进行任何强制转换或检查。

这是在隐藏成员

多态性意味着改变某些子类中的方法、属性实现,比如说“派生”或“继承”类。但是,无论如何,它的签名是不可变的

C#提供了一种隐藏成员的好方法:“new”关键字

您只需要在派生类中的访问修饰符前面加上“new”,就可以了。如果您不这样做,C#编译器会建议您这样做

顺便说一下,如果您的目标是使用多态性,那么您应该考虑泛型

您的基类应该有一个“TPriceValue”泛型参数,它将如下所示:

public class Product<TPriceValue>
{
    public virtual int Id { get; set; }
    public virtual TPriceValue PriceList { get; set; }

    public Product()
    {
        // NOTE/EDIT: You wouldn't initialize "PriceList" here...
    }
}
公共类产品
{
公共虚拟整数Id{get;set;}
公共虚拟定价列表{get;set;}
公共产品()
{
//注意/编辑:您不会在此处初始化“价格表”。。。
}
}
因此,如果希望price值为十进制,则应通过以下方式实例化类:

new Product<decimal>();
新产品();
或者,如果您希望将您的值作为另一个字典:

new Product<IDictionary<PriceType, decimal>>();
新产品();
如果我没有错的话,这是你的做法;)


编辑:对不起,我忘了更改基类中的某些内容,请再次检查此答案:)

这是隐藏成员

多态性意味着改变某些子类中的方法、属性实现,比如说“派生”或“继承”类。但是,无论如何,它的签名是不可变的

C#提供了一种隐藏成员的好方法:“new”关键字

您只需要在派生类中的访问修饰符前面加上“new”,就可以了。如果您不这样做,C#编译器会建议您这样做

顺便说一下,如果您的目标是使用多态性,那么您应该考虑泛型

您的基类应该有一个“TPriceValue”泛型参数,它将如下所示:

public class Product<TPriceValue>
{
    public virtual int Id { get; set; }
    public virtual TPriceValue PriceList { get; set; }

    public Product()
    {
        // NOTE/EDIT: You wouldn't initialize "PriceList" here...
    }
}
公共类产品
{
公共虚拟整数Id{get;set;}
公共虚拟定价列表{get;set;}
公共产品()
{
//注意/编辑:您不会在此处初始化“价格表”。。。
}
}
因此,如果希望price值为十进制,则应通过以下方式实例化类:

new Product<decimal>();
新产品();
或者,如果您希望将您的值作为另一个字典:

new Product<IDictionary<PriceType, decimal>>();
新产品();
如果我没有错的话,这是你的做法;)


编辑:对不起,我忘了更改基类中的某些内容,请再次检查以下答案:)

这种方法怎么样


public abstract class AProductBase<T>
{
  public IDictionary<string, T> PriceList { get; set; }
  ...
}

public class Product : AProductBase<decimal>
{
  ...
}

public enum PriceTypeEnum { ... }

public class SpecialProduct : AProductBase<IDictionary<PriceTypeEnum, decimal>>
{
  ...
}

公共抽象类产品库
{
公共IDictionary价格表{get;set;}
...
}
公共类产品:AProductBase
{
...
}
公共枚举PriceTypeEnum{…}
公共类SpecialProduct:APProductBase
{
...
}
也许这更符合你的要求


关于

这种方法怎么样


public abstract class AProductBase<T>
{
  public IDictionary<string, T> PriceList { get; set; }
  ...
}

public class Product : AProductBase<decimal>
{
  ...
}

public enum PriceTypeEnum { ... }

public class SpecialProduct : AProductBase<IDictionary<PriceTypeEnum, decimal>>
{
  ...
}

公共抽象类产品库
{
公共IDictionary价格表{get;set;}
...
}
公共类产品:AProductBase
{
...
}
公共枚举PriceTypeEnum{…}
公共类SpecialProduct:APProductBase
{
...
}
也许这更符合你的要求


关于

您不需要在派生类中创建第二个字典字段,请进一步解释为什么需要“将枚举类型约束为十进制”?我在财务部门工作,因此十进制您不需要在派生类中创建第二个字典字段,请进一步解释为什么需要“将枚举类型约束为十进制”?我在财务部门工作,因此产品构造函数中的赋值未编译:无法将源类型“System.Collections.Generic.Dictionary”转换为目标类型“TPrice”。我忘了提到,我还想将这些产品保存在另一个通用列表中。事实上,这是一个“输入错误”,将构造函数与此赋值分开。我只是编辑以删除它。为什么?既然TPrice是泛型类型,为什么要用字典设置“PriceList”?在创建“Product”实例时必须这样做,因为如果“TPriceValue”是十进制的,并且要设置字典,会发生什么情况?:)我已经猜到了。然后如何将这样的对象传递给方法。如何将产品和产品添加到IList???您需要使用相反的方法,但您不能