首页 > 学院 > 开发设计 > 正文

简单的反射

2019-11-14 16:22:28
字体:
来源:转载
供稿:网友

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

 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);                    }                }            }        }

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表