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

结果显示:

 

posted @ 2020-04-05 19:51  kevin860  阅读(2658)  评论(0编辑  收藏  举报