您的位置:首页 > 其它

通过反射获取匿名类型的属性和属性值

2016-11-28 19:21 211 查看
  

  static void Main(string[] args)

        {

             var Student1 = new { Name = "张三", Age = 11, Sex = "男", Address = "北京" }; 

             var Student2 = new { Name = "李四", Age = 12, Sex = "男", Address = "西安" };

             GetProperty(Student2);

             Console.Read();

             

        }

        public static void GetProperty(params object[] customObject)

        {

            foreach (object obj in customObject)

            {

                foreach (System.Reflection.PropertyInfo property in obj.GetType().GetProperties())

                {

                    Console.WriteLine(string.Format("PropertyName:{0},PropertyValue:{1}",

                        property.Name, property.GetValue(obj, null)));

                }

            }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: