【问题标题】:How to call a Generic method with dynamic properties in C#如何在 C# 中调用具有动态属性的通用方法
【发布时间】:2015-07-17 12:03:06
【问题描述】:

我有一些具有相似签名的方法,并试图在不使用接口的情况下将它们转换为一种通用方法。

 public List<MultiSelectDropdown> ConvertListOfJobStatusToDropdownListClickable(List<JobStatus> js) {
        var list = new List<MultiSelectDropdown>();
        if (js != null && js.Count >= 1) {
            list = js.Select(item => new MultiSelectDropdown { Name = item.StatusValue, Value = item.id.ToString() }).ToList();
        }
        return list;
    }


    public List<MultiSelectDropdown> ConvertListOfCUsersToDropdownListClickable(List<cUser> users) {
        var list = new List<MultiSelectDropdown>();
        if (users != null && users.Count >= 1) {
            list = users.Select(item => new MultiSelectDropdown { Name = item.User_Name, Value = item.Id.ToString() }).ToList();
        }
        return list;
    }

这就是我想做的;传入一个包含两个属性的列表。

List<MultiSelectDropdown> ddlForClientUsers = ConvertToMultiSelectDropdownList(listOfClientsForUser, n => n.Client_Id, v => v.Client);

List<MultiSelectDropdown> ddlForJobStatus = ConvertToMultiSelectDropdownList(listOfJobStatus, n => n.Id, v => v.JobName);

这是我尝试过的方法,但不确定如何让 item.propName 和 item.propValue 工作。

我在下面的方法中得到“无法解析”propName 和 propValue

这可能吗?

 public List<MultiSelectDropdown> ConvertToMultiSelectDropdownList<T, TPropertyName, TPropertyValue>(List<T> listOfT, Func<T, TPropertyName> propName, Func<T, TPropertyValue> propValue) {
var list = new List<MultiSelectDropdown>();
        if (listOfT != null && listOfT.Count >= 1) {
            list = listOfT.Select(item => new MultiSelectDropdown { Name = item.propName, Value = item.propValue }).ToList();
        }
        return list;
    }

public class MultiSelectDropdown {
    public string Name { get; set; }
    public string Value { get; set; }
    public bool IsChecked { get; set; }
}

【问题讨论】:

    标签: c# generics lambda generic-list


    【解决方案1】:

    因为 MultiSelectDropdown 的属性是字符串,所以您的函数也应该返回这些属性。要调用这些函数,您必须将它们写成 propName(item) 而不是 item.propName - 这是属性语法,并且您表示您不想使用接口。

    public List<MultiSelectDropdown> ConvertToMultiSelectDropdownList<T>(List<T> listOfT, Func<T, string> propName, Func<T, string> propValue) {
        var list = new List<MultiSelectDropdown>();
        if (listOfT != null && listOfT.Count >= 1) {
            list = listOfT.Select(item => new MultiSelectDropdown { Name = propName(item), Value = propValue(item) }).ToList();
        }
        return list;
    }
    

    【讨论】:

    • 或者,为了保持它的美观和通用性,在传入的 func 的结果上调用 .ToString() - 不过让日期玩得很好可能会很有趣 ;)
    【解决方案2】:

    你真的很接近,只是一个小错误。该行(重新格式化以防止滚动):

    list = listOfT.Select(item => new MultiSelectDropdown 
                                      { 
                                          Name = item.propName, 
                                          Value = item.propValue 
                                      }).ToList();
    

    需要:

    list = listOfT.Select(item => new MultiSelectDropdown 
                                      { 
                                          Name = propName(item), 
                                          Value = propValue(item)
                                      }).ToList();
    

    【讨论】:

      猜你喜欢
      • 2012-04-09
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 2014-04-27
      • 1970-01-01
      相关资源
      最近更新 更多