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 = ;
if (dtlName.Contains("price") || dtlName.Contains("amount"))
dit = ;
bb.SetValue(aa, Math.Round(Convert.ToDecimal(oldValue), dit, MidpointRounding.AwayFromZero));
}
Console.WriteLine($"子级属性名称:{dtlName},类型:{dtlType},值:{oldValue}");
}
}
}
}
}
测试方法:
var model = new Product
{
Id = "",
Name = "Test1",
Detail = new List<ProductDetail>
{
new ProductDetail{Id="" ,DtlId="",Number=12.3568M,Price=5.689M,Amount=70.2978352M},
new ProductDetail{Id="",DtlId="",Number=12.35M,Price=5.689M,Amount=70.2978352M},
new ProductDetail{Id="",DtlId="",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();
结果显示:
C# 反射获取属性值、名称、类型以及集合的属性值、类型名称的更多相关文章
- java中使用反射获取pojo(实体)类的全部字段值
说起反射.不得不说它实在是太强大了,通过反射就能够轻轻松松拿到各种东东,假设你想在项目中解除对某个类的依赖,能够考虑用反射. 今天跟大家分享的是通过java中的反射,获取pojo类的全部字段值. 为什 ...
- java中使用反射获取pojo(实体)类的所有字段值
出处:https://developer.aliyun.com/article/239346 说起反射,不得不说它实在是太强大了,通过反射就可以轻轻松松拿到各种东东,如果你想在项目中解除对某个类的依赖 ...
- org.reflections 接口通过反射获取实现类源码研究
org.reflections 接口通过反射获取实现类源码研究 版本 org.reflections reflections 0.9.12 Reflections通过扫描classpath,索引元数据 ...
- 面试题----入参两个Integer,无返回值,然后使这个两个值在调用函数后交换
我最近看到过一个比较好玩的面试题. 写个方法,入参两个Integer,无返回值,然后使这个两个值在调用函数后交换 很有意思的一个题目,引发我的深思,根据一路的学习过来,下面把实现代码贴出来,方便学习. ...
- 6-12 varchar和char 枚举类型enum 集合set
1 字符类型char和varchar #官网:https://dev.mysql.com/doc/refman/5.7/en/char.html #注意:char和varchar括号内的参 ...
- C# 反射获取属性类型及属性值,两个实体转换
一.两个实体数据转换 /// <summary> /// 为属性赋值 /// </summary> /// <typeparam name="T"&g ...
- c# 如何通过反射 获取\设置属性值
c# 如何通过反射 获取\设置属性值 //定义类public class MyClass{public int Property1 { get; set; }}static void Main(){M ...
- java利用反射获取类的属性及类型
java利用反射获取类的属性及类型. import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Map ...
- 反射获取属性DisplayName特性名字以及属性值
/// <summary> /// 反射获取所有DisplayName标记值 /// </summary> /// <typeparam name="T&quo ...
随机推荐
- c++中STL容器中的排序
1.c++STL中只有list自带了排序函数: (1).若list中存放的是int类型或者string类型,直接利用sort即可: list <int> list1; ...
- C++实验四
// 类graph的实现 #include "graph.h" #include <iostream> using namespace std; // 带参数的构造函数 ...
- Django 数据迁移
在1.6之前, Django只支持添加新的model到数据库, 而无法编辑或修改已经存在的model. 在当时, 这些Django缺失的功能可以通过South实现. 1. 新的命令 Django 1. ...
- node.js跨域
先上解决方法:在函数中添加(不要用xhr请求) // 只需要关心第二个参数res.setHeader('Access-Control-Allow-Origin', 'http://localhost: ...
- Python 基础知识(持续更新中)
内置数据类型: 整型 浮点型 字符串 布尔值 空值 None 列表 list 元组 tuple 字典 dict 集合 set ...
- SpringMVC中post请求参数注解@requestBody使用问题
一.httpClient发送Post 原文https://www.cnblogs.com/Vdiao/p/5339487.html public static String httpPostWithJ ...
- Mac OS环境下DOSBox汇编环境的搭建
平台: mac 工具: DOSBox(点击下载DOSBox官网) debug.edit.link.masm等等(点击下载masm汇编开发工具,提取码: skc8) 步骤: 1 .安装DOSBox: 解 ...
- 引擎设计跟踪(九.14.3.2) Deferred shading的后续实现和优化
最近完成了deferred shading和spot light的支持, 并作了一部分优化. 之前forward shading也只支持方向光, 现在也支持了点光源和探照光. 对于forward sh ...
- tf.Variable() 与tf.get_variable()的区别
每次调用 tf.Variable() 都会产生一个新的变量,变量名称是一个可选参数,运行命名相同,如果命名冲突会根据命名先后对名字进行处理, tf.get_variable()的变量名称是必填参数,t ...
- C# 程序修改config文件后,不重启程序刷新配置ConfigurationManager
基本共识: ConfigurationManager 自带缓存,且不支持 写入. 如果 通过 文本写入方式 修改 配置文件,程序 无法刷新加载 最新配置. PS. Web.config 除外:Web. ...