在C#中附加一个集合的StringBuilder扩展方法
-
20-08-2019 - |
题
在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表达式的工作。
解决方案
使用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));
}
其他提示
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。
我不知道你需要的是努力工作:
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();
当然,人需要重写的ToString()以产生用于一个Person对象正确的输出。
是这样的:
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%的病例specifiying拉姆达...
public static void AppendCollection<TItem>(this StringBuilder builder, IEnumerable<TItem> items)
{
AppendCollection(builder, items, x=>x.ToString());
}
我的版本:
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());
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的。这种方法你可以IT连锁:
static StringBuilder AppendCollection<T>(this StringBuilder sb,
IEnumerable<T> coll,
Func<T,string> action)
{
// same
return sb;
}
的字符串peopleAndOrders = sb.AppendCollection(人,p值=> p.ToString()) .AppendCollection(订单,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(人).AppendCollection(订单)的ToString();
这是什么方法想回去呢?我可以看到一个字符串,但为什么,如果你是附加到一个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)));
}
(其为相同布鲁诺孔德所述)
现在你不是真的需要它了:)