在轉成json時常常有動態屬性的必要,這時dictionary是很需要的
以下是個不錯的extension,來源在此
直接copy paste 了
-----------------------------------------------
public static class Extensions
{
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> items)
{
foreach (var item in items)
{
collection.Add(item);
}
}
public static dynamic ToDynamicObject(this IDictionary<string, object> source)
{
ICollection<KeyValuePair<string, object>> someObject = new ExpandoObject();
someObject.AddRange(source);
return someObject;
}
}
----------------------------------------------
很好用,推薦給c# codder們
沒有留言:
張貼留言