首页 > Windows开发 > 详细

C#反射

时间:2021-02-24 23:27:06      阅读:39      评论:0      收藏:0      [点我收藏+]
         
class UserInfo {
        public int ID { get; set; }
        public string Name { get; set; }
        public DateTime? CreateDate { get; set; }
        public decimal? Number { get; set; }
        public bool IsUse { get; set; }
    }

 


UserInfo userInfo = new UserInfo(); string values = string.Empty; foreach (System.Reflection.PropertyInfo p in userInfo.GetType().GetProperties()) { if (p.PropertyType == typeof(string)) { values += string.Format("{0}=‘{1}‘, ", p.Name, p.GetValue(userInfo)); } if (p.PropertyType == typeof(int)|| p.PropertyType == typeof(uint)) { values += string.Format("{0}={1},", p.Name, p.GetValue(userInfo)); } if (p.PropertyType == typeof(DateTime)) { values += string.Format("{0}=‘{1}‘, ", p.Name, p.GetValue(userInfo)); } if (p.PropertyType == typeof(decimal) || p.PropertyType == typeof(double)|| p.PropertyType == typeof(float)) { values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo)); } if (p.PropertyType == typeof(bool)) { values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo)); } if (p.PropertyType == typeof(sbyte)) { values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo)); } if (p.PropertyType == typeof(byte) || p.PropertyType == typeof(short) || p.PropertyType == typeof(ushort) ) { values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo)); } if (p.PropertyType == typeof(long) || p.PropertyType == typeof(ulong)) { values += string.Format("{0}={1}, ", p.Name, p.GetValue(userInfo)); } // values +=string.Format( "{0}={1},", p.Name, p.GetValue(userInfo)); // Console.WriteLine("Name:{0} Value:{1}", p.Name, p.GetValue(userInfo)); } Console.WriteLine(values);

C#反射

原文:https://www.cnblogs.com/XiaoHongMao1/p/14442913.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!