C# 反射获取属性值、名称、类型以及集合的属性值、类型名称

实体类

class Product
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public List<ProductDetail> Detail { get; set; }
        public List<ProductComment> Comment { get; set; }
    }
    class ProductDetail
    {
        public string DtlId { get; set; }
        public string Id { get; set; }
        public decimal Number { get; set; }
        public decimal Price { get; set; }
        public decimal Amount { get; set; }
    }
    class ProductComment
    {
        public string DtlId { get; set; }
        public string Id { get; set; }
        public string Comment { get; set; }
    }

反射获取属性值等,中间加了小数位数保留的操作(黄色部分) 

 

static void FromatDitits<T>(T model)
        {
            var newType = model.GetType();
            foreach (var item in newType.GetRuntimeProperties())
            {
                var type = item.PropertyType.Name;
                var IsGenericType = item.PropertyType.IsGenericType;
                var list = item.PropertyType.GetInterface("IEnumerable", false);
                Console.WriteLine($"属性名称:{item.Name},类型:{type},值:{item.GetValue(model)}");
                if (IsGenericType && list != null)
                {
                    var listVal = item.GetValue(model) as IEnumerable<object>;
                    if (listVal == null) continue;
                    foreach (var aa in listVal)
                    {
                        var dtype = aa.GetType();
                        foreach (var bb in dtype.GetProperties())
                        {
                            var dtlName = bb.Name.ToLower();
                            var dtlType = bb.PropertyType.Name;
                            var oldValue = bb.GetValue(aa);
                            if (dtlType == typeof(decimal).Name)
                            {
                                int dit = 4;
                                if (dtlName.Contains("price") || dtlName.Contains("amount"))
                                    dit = 2;
                                bb.SetValue(aa, Math.Round(Convert.ToDecimal(oldValue), dit, MidpointRounding.AwayFromZero));
                            }
                            Console.WriteLine($"子级属性名称:{dtlName},类型:{dtlType},值:{oldValue}");
                        }
                    }
                }
            }
        }

测试方法:

var model = new Product
            {
                Id = "111",
                Name = "Test1",
                Detail = new List<ProductDetail>
                {
                    new ProductDetail{Id="111" ,DtlId="1",Number=12.3568M,Price=5.689M,Amount=70.2978352M},
                    new ProductDetail{Id="111",DtlId="2",Number=12.35M,Price=5.689M,Amount=70.2978352M},
                    new ProductDetail{Id="111",DtlId="3",Number=12.358M,Price=5.689M,Amount=70.304662M},
                }
            };
            FromatDitits<Product>(model);
            Console.WriteLine("----------------------------");
            foreach (var item in model.Detail)
            {
                Console.WriteLine($"Number值为:{item.Number},Price值为:{item.Price},Amount值为:{item.Amount}");
            }

            Console.ReadKey();

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值