[英]Make generic method with template with IEnumerable<T>
我希望將兩個 function 結合起來,它們具有相似的代碼但變量類型不同。 我想在IEnumerable
中使用 T 類型,但它似乎不起作用。
方法一:
public static IList<ListItem> AppendTopMakesToList(this IEnumerable<ListItem> options, bool appendSeparatorRow = true)
{
if (options == null || string.IsNullOrEmpty(Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes")))
return options.ToList();
var topmakes = Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes").Split('|').ToList().ConvertAll(d => d.ToLower());
var filteredMakes = options.Where(x => topmakes.Any(y => y.Contains(x.Text.ToLower()))).ToList();//getting all the makes from the listItems
if (appendSeparatorRow)
{
var separatorListItem = new ListItem("------------------", "------------------", false);
separatorListItem.Attributes.Add("disabled", "true"); //disabling the separator item so that it can't be selected
filteredMakes.Add(separatorListItem);
}
var items = options.ToList();
items.InsertRange(0, filteredMakes);
return items;
}
方法二:
public static IList<Make> AppendTopMakesToList(this IEnumerable<Make> options, bool appendSeparatorRow = true)
{
if (options == null || string.IsNullOrEmpty(Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes")))
return options.ToList();
var topmakes = Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes").Split('|').ToList().ConvertAll(d => d.ToLower());
var filteredMakes = options.Where(x => topmakes.Any(y => y.Contains(x.Code.ToLower()))).ToList();//getting all the makes from the listItems
if (appendSeparatorRow)
{
var separatorListItem = new Make()
{
Code = "------------------",
Description = "------------------"
};
filteredMakes.Add(separatorListItem);
}
var items = options.ToList();
items.InsertRange(0, filteredMakes);
return items;
}
}
}
這些函數與返回類型IList<Make>
和IList<ListItem>
的部分完全相同,該類型通過IEnumerable<Make>
和IEnumerable<ListItem>
的參數類型傳遞。
我會做類似的事情:
public static IList<T> AppendItems<T>(this IEnumerable<T> options, Action<T> blankFactory, bool appendSeparatorRow = true)
{
if (options == null || string.IsNullOrEmpty(Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes"))) return options.ToList();
var topmakes = Sitecore.Configuration.Settings.GetSetting("RACQ.JoinMembership.Top10CarMakes").Split('|').ToList().ConvertAll(d => d.ToLower());
var filteredMakes = options.Where(x => topmakes.Any(y => y.Contains(x.Code.ToLower()))).ToList();//getting all the makes from the listItems
if (appendSeparatorRow)
{
var separatorListItem = blankFactory();
filteredMakes.Add(separatorListItem);
}
var items = options.ToList();
items.InsertRange(0, filteredMakes);
return items;
}
然后你可以像這樣使用它:
var whatever = new List<ListItem>():
AppendItems(whatever, () => {
var separatorListItem = new ListItem("------------------", "------------------", false);
separatorListItem.Attributes.Add("disabled", "true");
return separatorListItem;
}, true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.