C#通过反射获取相应的字段和值

本文介绍了一种使用C#反射机制获取对象属性名称及对应值的方法,通过遍历对象的所有属性并根据属性类型格式化输出,适用于不同类型的属性值获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#通过反射获取相应的字段和值

https://www.cnblogs.com/bmyblogs/articles/9346843.html

C#通过反射获取相应的字段和值

代码比较简单,只作为简单的例子参考

首先先看运行的代码:

 

复制代码

 class Program
    {
        static void Main(string[] args)
        {
            UserInfo userInfo = new UserInfo();
            userInfo.ID = 1;
            userInfo.Name = "bailey";
            userInfo.CreateDate = DateTime.Now;
            userInfo.Number = Convert.ToDecimal(456.6467);

            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);
            Console.ReadLine();
        }

    }

复制代码

 

再看对象:

 

复制代码

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

复制代码

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值