静态类型对象可枚举集合声明
static typed object enumerable collection declaration
我有几个编辑器。每个编辑器都有特殊性,例如数据库中的特定字段类型或可编辑或不可编辑。
所以我需要一个具有名称和属性的编辑器类型集合。我需要能够使用集合
填充组合框
如何声明每种类型并获得类型列表?
我试试这个
public enum TemplateType
{TextBox,ColorPicker};
public class TabloidBaseControl
{
public static DbType DefaultFieldType { get { return DbType.String; } }
public static int? DefaultFieldLength { get { return 20; } }
public static int? DefaultFieldDecimal { get { return null; } }
}
public class TCColorPicker:TabloidBaseControl
{
public new static int? DefaultFieldLength = 7;
}
public class TCTextBox: TabloidBaseControl
{
public new static int? DefaultFieldLength = 20;
}
public static Dictionary<TemplateType, TabloidBaseControl> TabloidControlList = new Dictionary<TemplateType, TabloidBaseControl> {
{TemplateType.TextBox,new TCTextBox()},
{TemplateType.ColorPicker,new TCColorPicker() }
};
这样做对吗?
您没有正确执行 class 继承。在基础 class 上使用虚拟属性,并在派生的 class 中覆盖适当的属性,如下所示:
public enum TemplateType
{TextBox,ColorPicker};
public class TabloidBaseControl
{
public virtual DbType DefaultFieldType
{
get { return DbType.String; }
}
public virtual int? DefaultFieldLength
{
get { return 20; }
}
public virtual int? DefaultFieldDecimal
{
get { return null; }
}
}
public class TCColorPicker : TabloidBaseControl
{
public override int? DefaultFieldLength
{
get { return 7; }
}
}
public class TCTextBox : TabloidBaseControl
{
public override int? DefaultFieldLength
{
get { return 20; }
}
}
是的,您的字典总是 return 对象作为基础 class 的一个实例,因为它就是这样定义的。但是,对象的运行时类型将是您添加的任何派生 class,因此正确的值将从 属性 访问器 return 编辑。
我有几个编辑器。每个编辑器都有特殊性,例如数据库中的特定字段类型或可编辑或不可编辑。
所以我需要一个具有名称和属性的编辑器类型集合。我需要能够使用集合
填充组合框如何声明每种类型并获得类型列表?
我试试这个
public enum TemplateType
{TextBox,ColorPicker};
public class TabloidBaseControl
{
public static DbType DefaultFieldType { get { return DbType.String; } }
public static int? DefaultFieldLength { get { return 20; } }
public static int? DefaultFieldDecimal { get { return null; } }
}
public class TCColorPicker:TabloidBaseControl
{
public new static int? DefaultFieldLength = 7;
}
public class TCTextBox: TabloidBaseControl
{
public new static int? DefaultFieldLength = 20;
}
public static Dictionary<TemplateType, TabloidBaseControl> TabloidControlList = new Dictionary<TemplateType, TabloidBaseControl> {
{TemplateType.TextBox,new TCTextBox()},
{TemplateType.ColorPicker,new TCColorPicker() }
};
这样做对吗?
您没有正确执行 class 继承。在基础 class 上使用虚拟属性,并在派生的 class 中覆盖适当的属性,如下所示:
public enum TemplateType
{TextBox,ColorPicker};
public class TabloidBaseControl
{
public virtual DbType DefaultFieldType
{
get { return DbType.String; }
}
public virtual int? DefaultFieldLength
{
get { return 20; }
}
public virtual int? DefaultFieldDecimal
{
get { return null; }
}
}
public class TCColorPicker : TabloidBaseControl
{
public override int? DefaultFieldLength
{
get { return 7; }
}
}
public class TCTextBox : TabloidBaseControl
{
public override int? DefaultFieldLength
{
get { return 20; }
}
}
是的,您的字典总是 return 对象作为基础 class 的一个实例,因为它就是这样定义的。但是,对象的运行时类型将是您添加的任何派生 class,因此正确的值将从 属性 访问器 return 编辑。