【发布时间】:2013-06-17 04:42:48
【问题描述】:
我正在开发一个通用搜索表单,该表单中的搜索控件取决于 <T>properties 的类型,例如,如果 T 是 Order,
public class Order
{
public string OrderNumber {get; set;} // search control is 1 TextBox
public decimal OrderWeight {get; set;} // search controls are 2 TextBox (for accepting a range)
}
搜索表单会是这样的
我在我的表单中使用了这个语句来决定每个T 属性的适当控件:
if (propertyType.Name == "System.String")
InsertOneTextBox(paramInfo);
else
if(propertyType.Name == "System.Int32" || propertyType.Name == "System.Decimal")
InsertTwoTextBoxs(paramInfo);
else
if(propertyType.Name == "System.DateTime")
InsertTwoDateTimePickers(paramInfo);
else
if(propertyType.Name == someotherconditions)
InsertOneComboBox(paramInfo);
....
是否有任何最佳做法可以避免使用 if elses 或 switch case 来决定为每种属性类型设置哪些适当的控件?
【问题讨论】:
-
您能在
if-else示例中放置大括号吗? -
@IAbstract:我编辑了帖子。
标签: c# design-patterns generics if-statement switch-statement