Skip to main content
 首页 » 编程设计

c#之如何删除表达式条件

2024年02月13日15davidwang456

我有一个很长的条件声明:

  public static DataGridColumn CreateAppropreateColumn(string path, PropertyInfo info, string header, IRepository repository) 
    { 
        DataGridColumn column = null; 
 
        if (info.GetCustomAttributes(typeof(DbComboBoxAttribute), true).Any()) 
        { 
            column = CreateComboBoxColumn(path, info, header, repository); 
        } 
        else if (info.GetCustomAttributes(typeof(DescribedByteEnumComboBoxAttribute), true).Any()) 
        { 
            column = CreateEnumComboBoxColumn(path, info, header); 
        } 
        else if (info.GetCustomAttributes(typeof(DropDownLazyLoadingDataGridAttribute), true).Any()) 
        { 
            column = CreateDataGridDropDownLazyLoadingDataGridColumn(path, info, header, repository); 
        } 
        else if (info.GetCustomAttributes(typeof(DropDownTreeViewAttribute), true).Any()) 
        { 
            column = CreateDataGridTreeViewColumn(path, info, header, repository); 
        } 
        //Other controls (Like drop down panels ... and so on ) can add here . . . 
        //} 
        else if (info.GetCustomAttributes(typeof(DatePickerAttribute), true).Any()) 
        { 
            column = CreateDataGridDateColumn(path, info, header); 
        } 
        else if (info.GetCustomAttributes(typeof(YearPickerAttribute), true).Any()) 
        { 
            column = CreateDataGridYearColumn(path, info, header); 
        } 
        else if (info.PropertyType == typeof(bool) || info.PropertyType == typeof(bool?)) 
        { 
            column = CreateDataGridCheckBoxColumn(path, info, header); 
        } 
        else 
        { 
            column = CreateTextBoxColumn(path, info, header); 
        } 
return Column; 
 
} 

我可以删除 ifs 吗?

谢谢。

请您参考如下方法:

我经常做这样的事情的一种方法是创建一个字典,将类型映射到一个创建结果的函数,(你的字典将是 Dictionary<Type, Func<string, PropertyInfo, string, IRepository, DataGridColumn>> ),然后迭代查找现有的元素属性。

由于遍历继承链上的属性以查找特定类型的属性需要做一些工作,因此遍历它一次以创建属性列表,然后遍历它并调用如果你在字典中找到一个合适的功能。只有在字典(或属性)用完之后,您才需要拥有 if .

map = new Dictionary<Type, Func<string, PropertyInfo, string, IRepository, DataGridColumn>> 
{ 
    { typeof(DbComboBoxAttribute), CreateComboBoxColumn }, // no lambda needed if CreateComboBoxColumn already matches our Func<,,,,> type 
    { typeof(DescribedByteEnumComboBoxAttribute), (p, i, h, r) => CreateEnumComboBoxColumn(p, i, h) }, 
    ... 
}; 

然后像这样使用 map :

public static DataGridColumn CreateAppropriateColumn(string path, PropertyInfo info, string header, IRepository repository) 
{ 
    Func<string, PropertyInfo, string, IRepository, DataGridColumn> 
        colfunc = null; 
 
    // iterate over all the attributes, looking for one in our dictionary; 
    // use Attribute.GetCustomAttributes because it doesn't ignore 'inherit' 
    foreach (var attr in Attribute.GetCustomAttributes(info, true)) 
        if (map.TryGetValue(attr.GetType(), out colfunc)) 
            return colfunc(path, info, header, repository); 
 
    if (info.PropertyType == typeof(bool) || info.PropertyType == typeof(bool?)) 
        return CreateDataGridCheckBoxColumn(path, info, header); 
 
    return CreateTextBoxColumn(path, info, header); 
}