如何在 C# 中调用具有动态属性的泛型方法
How to call a Generic method with dynamic properties in C#
我有几个具有相似签名的方法,并试图在不使用接口的情况下将它们转换为一个通用方法。
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 起作用的方法。
我在下面的方法中得到 "Cannot resolve" 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; }
}
因为您的 MultiSelectDropdown 的属性是字符串,所以您的函数也应该 return 那些。要调用这些函数,您必须将它们编写为 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;
}
你真的很接近,只是一个小错误。该行(重新格式化以防止滚动):
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();
我有几个具有相似签名的方法,并试图在不使用接口的情况下将它们转换为一个通用方法。
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 起作用的方法。
我在下面的方法中得到 "Cannot resolve" 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; }
}
因为您的 MultiSelectDropdown 的属性是字符串,所以您的函数也应该 return 那些。要调用这些函数,您必须将它们编写为 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;
}
你真的很接近,只是一个小错误。该行(重新格式化以防止滚动):
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();