我从Dictionary 里面拿到 一个object的实体类,但是我不知道是什么实体类,我想取得这个实体类里面的所有字段的值,应该怎么弄啊!
Dictionary<string, object> dic = new Dictionary<string, object>();            Base.UI.Model.Order or = new Base.UI.Model.Order();
            or.AD_ID = 100;
            or.Buy_nick = "test";
            dic["test"] = or;到时候会传进来各种实体类,我不知道是那个实体(就是我不知道Base.UI.Model.Order是这个类型的有可能是别的类型的),但是我要求出所有的值!

解决方案 »

  1.   

    如果在无条件的object下进行处理,没有任何人或程序能做到
    对object要有要求,如需要实现某个interface或继承某个类,
    ....
      

  2.   

    顶楼上可以用反射类似下面这种方式,反射好东西啊~using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;namespace TestThree
    {
        public class Program
        {
            /// <summary>
            /// 测试方法
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                IDictionary<string, object> testDict = new Dictionary<string, object>();
                testDict.Add("测试", new Student());            object objYouGet = testDict["测试"];
                Type t = objYouGet.GetType();            PropertyInfo[] proInfos = t.GetProperties();
                foreach (PropertyInfo item in proInfos)
                {
                    Console.WriteLine("字段名称:{0}", item.Name);
                    Console.WriteLine("字段值:{0}", item.GetValue(objYouGet, null));
                    Console.WriteLine("字段类型:{0}", item.PropertyType);
                    Console.WriteLine("--------------------");
                }            Console.ReadLine();
            }
        }
        /// <summary>
        /// 测试类
        /// </summary>
        public class Student
        {
            public Student()
            {
                this.Id = 0;
                this.ChineseName = "测试中文名";
                this.EnglishName = "Test EnglishName";
                this.Score = 100;
            }        public int Id { get; set; }        public string ChineseName { get; set; }        public string EnglishName { get; set; }        public double Score { get; set; }
        }
    }