I have this question from Most efficient Dictionary.ToString() with formatting?, but my problem is if V is List, how to make it works. Now my solution is, change
itemString.AppendFormat(format, item.Key, item.Value);
to
itemString.AppendFormat(format, item.Key, item.Value.ToDelimitedStr());
here is code of ToDelimitedStr:
public static string ToDelimitedStr<T>(this T source)
{
// List<string>
if (source is IList &&
source.GetType().IsGenericType)
{
Type t1 = source.GetType().GetGenericArguments()[0];
if (t1.Name == "String")
((IEnumerable<string>)source).ToDelimitedString();
}
return source.ToString();
}
which only work for List<string>
. How I can make it more generic?
Also, I'm thinking, maybe I should not work on the top of
public string DictToString<T, V>(IEnumerable<KeyValuePair<T, V>> items, string format)
I should create a new version like
public string DictListToString<T, List<V>>(IEnumerable<KeyValuePair<T, List<V>>> items, string format)
how about that?
Thank you so much
Wes