匿名对象工具类

public class AnonymousObject
{
    private object obj;

    public AnonymousObject(object obj)
    {
        this.obj = obj;
    }

    public Dictionary<string, object> ToDictionary()
    {
        var dict = new Dictionary<string, object>();

        System.ComponentModel.PropertyDescriptorCollection pds = System.ComponentModel.TypeDescriptor.GetProperties(this.obj);

        foreach (System.ComponentModel.PropertyDescriptor pd in pds)
        {
            dict.Add(pd.Name, pd.GetValue(this.obj));
        }

        return dict;
    }
}

 

posted on 2010-09-02 01:34  王庭安  阅读(299)  评论(0编辑  收藏  举报