6

在 C# 中,我正在尝试为 StringBuilder 构建一个名为 AppendCollection() 的扩展方法,它可以让我这样做:

var sb1 = new StringBuilder();
var sb2 = new StringBuilder();
var people = new List<Person>() { ...init people here... };
var orders = new List<Orders>() { ...init orders here... };

sb1.AppendCollection(people, p => p.ToString());
sb2.AppendCollection(orders, o => o.ToString());

string stringPeople = sb1.ToString();
string stringOrders = sb2.ToString();

stringPeople 最终会为列表中的每个人加上一行。每一行都是 p.ToString() 的结果。对于 stringOrders 也是如此。我不太确定如何编写代码以使 lambda 与泛型一起使用。

4

7 回答 7

11

使用Func<T,string>委托。

public static void AppendCollection<T>(this StringBuilder sb, 
                                       IEnumerable<T> collection, Func<T, string> method) {
   foreach(T x in collection) 
       sb.AppendLine(method(x));
}
于 2008-12-09T19:08:26.407 回答
5
 public static void AppendCollection<T>(this StringBuilder builder, IEnumerable<T> list, Func<T,string> func)
        {
            foreach (var item in list)
            {
                builder.AppendLine(func(item));
            }
        }

我不会返回一个字符串,我只会将它附加到传入的原始 Stringbuilder 中。

于 2008-12-09T19:14:29.827 回答
3

我不确定你是否需要那么努力:

 public static void AppendCollection( this StringBuilder builder,
                                      ICollection collection )
 {
     foreach (var item in collection)
     {
        builder.AppendLine( Convert.ToString( item ) );
     }
 }

用作

 List<Person> people = ...

 StringBuilder builder = new StringBuilder();
 builder.AppendCollection( people );
 var s = builder.ToString();

当然,Person 需要重写 ToString() 来为 Person 对象生成正确的输出。

于 2008-12-09T19:09:03.987 回答
3

就像是:

  public static void AppendCollection<TItem>(this StringBuilder builder, IEnumerable<TItem> items, Func<TItem, string> valueSelector)
  {
       foreach(TItem item in items)
       {  
            builder.Append(valueSelector(item));
       }
  }

我会添加一个有用的默认值,以在 90% 的情况下保存指定 lambda...

   public static void AppendCollection<TItem>(this StringBuilder builder, IEnumerable<TItem> items)
  {
      AppendCollection(builder, items, x=>x.ToString());
   }
于 2008-12-09T19:09:40.040 回答
3

我的版本:

    public static string AppendCollection<T>(this StringBuilder sb, IEnumerable<T> enumerable, Func<T, string> method)
    {
        List<T> l = new List<T>(enumerable);
        l.ForEach(item => sb.AppendLine(method(item)));
        return sb.ToString();
    }

但在这种情况下你不应该返回一个字符串。我更喜欢以下内容:

    public static void AppendCollection<T>(this StringBuilder sb, IEnumerable<T> enumerable, Func<T, string> method)
    {
        List<T> l = new List<T>(enumerable);
        l.ForEach(item => sb.AppendLine(method(item)));
    }

像这样使用:

        sb.AppendCollection(people, p => p.ToString());
        sb.AppendCollection(orders, o => o.ToString());
        Console.WriteLine(sb.ToString());
于 2008-12-09T19:29:09.970 回答
2

这个方法应该返回什么?我可以看到一个字符串,但是如果您要附加到 StringBuilder,为什么?

您尝试做的事情相当简单,但您需要准确地解释您想要什么。

更新:

这是我的看法。如果您只是要传入一个新的 StringBuilder 并返回一个字符串,那么为此使用扩展方法是愚蠢且毫无意义的。

更新 2:

既然我看到了这种用法,那么您所做的就是不好的做法。理想情况下,您应该做的是:

public static string Print<T>(this IEnumerable<T> col, Func<T,string> printer)
{
  var sb = new StringBuilder();
  foreach (T t in col)
  {
    sb.AppendLine(printer(t));
  }
  return sb.ToString();
}

string[] col = { "Foo" , "Bar" };
string lines = col.Print( s => s);

更新 3:

进一步澄清后:

public static void AppendCollection<T>(this StringBuilder sb, 
   List<T> col, Func<T,string> printer)
{
  col.ForEach( o => sb.AppendLine(printer(o)));
}

(这与布鲁诺康德所说的相同)

现在你真的不再需要它了:)

于 2008-12-09T19:07:45.113 回答
2
static class SBExtention
{
  static string AppendCollection<T>(this StringBuilder sb, 
                                    IEnumerable<T> coll, 
                                    Func<T,string> action)
  {
       foreach(T t in coll)
       {
          sb.Append(action(t));
          sb.Append("\n");
       }
       return sb.ToString();

  }
}

但是,我认为让它返回 StringBuilder 会更好。这样你就可以链接它:

  static StringBuilder AppendCollection<T>(this StringBuilder sb, 
                                    IEnumerable<T> coll, 
                                    Func<T,string> action)
  {
       // same
       return sb;

  }

字符串 peopleAndOrders = sb.AppendCollection(people, p => p.ToString()) .AppendCollection(orders, o => o.ToString()).ToString();

我同意 Jennifer 关于默认情况的观点:

   public static StringBuilder AppendCollection<TItem>(
                  this StringBuilder builder, 
                  IEnumerable<TItem> items)
  {
      return AppendCollection(builder, items, x=>x.ToString());
   }

字符串 peopleAndOrders = sb.AppendCollection(people).AppendCollection(orders).ToString();

于 2008-12-09T19:11:54.590 回答