给定此方法,创建的结果字符串将删除连续的空值。下面??
被错误使用了吗?它的行为似乎是在返回
public static IEnumerable<string> ToCsv<T>(string separator, IEnumerable<T> objectlist)
{
FieldInfo[] fields = typeof(T).GetFields();
PropertyInfo[] properties = typeof(T).GetProperties();
yield return String.Join(separator, fields.Select(f => f.Name).Union(properties.Select(p => p.Name)).ToArray());
foreach (var o in objectlist)
{
var pp = properties.Select(
p => (p.GetValue(o, null) ?? String.Empty));
var val = string.Join(separator, fields.Select(f => (f.GetValue(o)).ToString() )
.Union(pp).ToArray());
;
yield return val;
}
}
这是我的数组
[0] “0001020003”
[1]“鲍勃琼斯”
[2] NULL
[3] NULL
[4]“长船”
使用该数组创建的连接字符串是......
“0001020003,Bob Jones ,, Longboat”