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
    评论
要判断一个对象集合是否包含某个属性,可以使用 LINQ 查询语句和反射来实现。具体实现步骤如下: 1. 使用 LINQ 查询语句从对象集合中筛选出包含指定属性的对象。 2. 使用反射获取对象的属性列表。 3. 遍历对象集合中的每个对象,判断对象的指定属性是否等于目标。 4. 如果对象包含指定属性,则返回 true,否则返回 false。 以下是示例代码: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Reflection; class Program { static void Main(string[] args) { List<Person> people = new List<Person> { new Person { Name = "Alice", Age = 28 }, new Person { Name = "Bob", Age = 30 }, new Person { Name = "Charlie", Age = 35 }, }; bool hasAge30 = HasPropertyValue(people, "Age", 30); Console.WriteLine("Has Age=30 property value: " + hasAge30); bool hasAge40 = HasPropertyValue(people, "Age", 40); Console.WriteLine("Has Age=40 property value: " + hasAge40); Console.ReadKey(); } static bool HasPropertyValue(IEnumerable<object> collection, string propertyName, object propertyValue) { var type = collection.GetType().GetGenericArguments()[0]; var properties = type.GetProperties(); var property = properties.FirstOrDefault(p => p.Name == propertyName); if (property == null) { return false; } return collection.Any(obj => property.GetValue(obj) == propertyValue); } } class Person { public string Name { get; set; } public int Age { get; set; } } ``` 在上面的示例中,我们定义了一个包含 Name 和 Age 两个属性的 Person ,并创建了一个包含三个 Person 对象的 List。然后,我们分别判断该 List 是否包含 Age=30 和 Age=40 两个属性。最终输出结果为: ``` Has Age=30 property value: True Has Age=40 property value: False ``` 可以看到,HasPropertyValue 方法可以判断对象集合是否包含指定的属性
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值