C#获取类的属性并赋值

            Type t = typeof(B_InventoryRecord);
            var properties = t.GetProperties();
            var p = list[0];
            foreach (var item in properties)
            {
                string value = (string)t.GetProperty(item.Name).GetValue(p, null);
                res.Add(value);
            }

这里有一些缺点。

缺点一:有些类的值不需要赋值,或者和数据库的字段不对应。这个可以使用Attribute标签加一些限制

缺点二:GetValue()方法获取的值都是object类型

记录编程的点滴,体会学习的乐趣
原文地址:https://www.cnblogs.com/AduBlog/p/14276338.html