码迷,mamicode.com
首页 > 其他好文 > 详细

简单的反射

时间:2014-10-28 15:15:49      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   for   sp   div   on   art   

想起来研究下简单的反射,然后就在网上找了点资料,也找了点代码,大致如下:

 public static void PrintProperties<T>(T t)
        {
            if (t == null)
            {
                return;
            }

            PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            if (properties.Length <= 0)
            {
                return;
            }

            foreach (PropertyInfo item in properties)
            {
                string name = item.Name;
                object value = item.GetValue(t, null);
                if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
                {
                    Console.WriteLine("{0}:{1}", name, value);
                }
                else
                {
                    foreach (PropertyInfo itemsub in value.GetType().GetProperties())
                    {

                        PrintProperties(value);
                    }
                }
            }
        }

 

简单的反射

标签:style   blog   color   ar   for   sp   div   on   art   

原文地址:http://www.cnblogs.com/bobo-pcb/p/4056854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!