【问题标题】:Make generic method with template with IEnumerable<T>使用 IEnumerable<T> 使用模板制作通用方法
【发布时间】:2019-10-25 00:32:26
【问题描述】:

我希望将两个具有相似代码但变量类型不同的函数结合起来。我想在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&lt;Make&gt;IList&lt;ListItem&gt; 的返回类型完全相同,后者是在IEnumerable&lt;Make&gt;IEnumerable&lt;ListItem&gt; 的参数类型上传递的。

【问题讨论】:

    标签: c# templates


    【解决方案1】:

    我会这样做:

    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);
    
    

    【讨论】:

    • 也不能使用whatever.AppendItems(()=&gt;{// blah blah})调用,因为第一个参数有this关键字。这两种方式都可以:D
    • 是的,@keysl 是对的,这是一种扩展方法,所以你也可以使用上面的语法。或using static 和我写的。或指定类名,如WhateverExtensions. 并以这种方式调用它。 C# 很灵活 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 2013-05-05
    • 2011-10-19
    相关资源
    最近更新 更多