您的位置:首页 > 其它

LinQ 定义带有返回类型的扩展方法3.2

2014-07-12 19:18 204 查看
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;

namespace ExtensionWithReturn
{
class Program
{
static void Main(string[] args)
{
var songs = new
{
Artist = "Green Day",
Song = "Wake Me Up When September Ends"
};
Console.WriteLine(songs.Dump());
Console.ReadLine();
}
}
public static class Dumper
{
public static string Dump(this Object o)
{
PropertyInfo[] properties = o.GetType().GetProperties();
StringBuilder builder = new StringBuilder();
foreach (PropertyInfo p in properties)
{
try
{
builder.AppendFormat(string.Format("Name: {0}, Value: {1}", p.Name, p.GetValue(o, null)));
}
catch
{
builder.AppendFormat(string.Format("Name: {0}, Value: {1}", p.Name, "unk."));
}
builder.AppendLine();
}
return builder.ToString();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐