实体类

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# 反射获取属性值、名称、类型以及集合的属性值、类型名称的更多相关文章

  1. java中使用反射获取pojo(实体)类的全部字段值

    说起反射.不得不说它实在是太强大了,通过反射就能够轻轻松松拿到各种东东,假设你想在项目中解除对某个类的依赖,能够考虑用反射. 今天跟大家分享的是通过java中的反射,获取pojo类的全部字段值. 为什 ...

  2. java中使用反射获取pojo(实体)类的所有字段值

    出处:https://developer.aliyun.com/article/239346 说起反射,不得不说它实在是太强大了,通过反射就可以轻轻松松拿到各种东东,如果你想在项目中解除对某个类的依赖 ...

  3. org.reflections 接口通过反射获取实现类源码研究

    org.reflections 接口通过反射获取实现类源码研究 版本 org.reflections reflections 0.9.12 Reflections通过扫描classpath,索引元数据 ...

  4. 面试题----入参两个Integer,无返回值,然后使这个两个值在调用函数后交换

    我最近看到过一个比较好玩的面试题. 写个方法,入参两个Integer,无返回值,然后使这个两个值在调用函数后交换 很有意思的一个题目,引发我的深思,根据一路的学习过来,下面把实现代码贴出来,方便学习. ...

  5. 6-12 varchar和char 枚举类型enum 集合set

    1       字符类型char和varchar #官网:https://dev.mysql.com/doc/refman/5.7/en/char.html #注意:char和varchar括号内的参 ...

  6. C# 反射获取属性类型及属性值,两个实体转换

    一.两个实体数据转换 /// <summary> /// 为属性赋值 /// </summary> /// <typeparam name="T"&g ...

  7. c# 如何通过反射 获取\设置属性值

    c# 如何通过反射 获取\设置属性值 //定义类public class MyClass{public int Property1 { get; set; }}static void Main(){M ...

  8. java利用反射获取类的属性及类型

    java利用反射获取类的属性及类型. import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Map ...

  9. 反射获取属性DisplayName特性名字以及属性值

    /// <summary> /// 反射获取所有DisplayName标记值 /// </summary> /// <typeparam name="T&quo ...

随机推荐

  1. 更改手机系统的User-Agent & okhttp

    okhttp 和 volley 1. 之前用的是volley,其中一部分功能,比如User-Agent,是系统去处理的,改成okhttp库后,这部分功能需要浏览器自己处理 2. 具体区别可以参考: h ...

  2. 报文 HTTP HTTPS

    报文是网络中交换与传输的数据单元,即站点一次性要发送的数据块.报文包含了将要发送的完整的数据信息,其长短很不一致,长度不限且可变. 报文也是网络传输的单位,传输过程中会不断的封装成分组.包.帧来传输, ...

  3. 使用maven整合spring+springmvc+mybatis

    使用maven整合spring+springmvc+mybatis 开发环境: 1. jdk1.8 2. eclipse4.7.0 (Oxygen) 3. mysql 5.7 在pom.xml文件中, ...

  4. c# excel xls保存

    public HSSFWorkbook Excel_Export(DataTable query,string title,int[] rowweight,string[] rowtitle) { H ...

  5. [Tom and Bag][需要记录过程的dp]

    http://acm.beihua.edu.cn/problem/1007 Tom and Bag   Description Tom is the most handsome CCPC contes ...

  6. eclipse的基本使用和配置

    在workspace中src目录下的文件是java文件,bin目录下是class文件 1:基本使用 A:创建Java项目: 点击File或者在最左侧空白处,选择Java项目,在界面中写一个项目名称,然 ...

  7. JS写法 数值与字符串的相互转换 取字符中的一部分显示 正则表达规则

    http://www.imooc.com/article/15885 正则表达规则 <script type="text/javascript"> </scrip ...

  8. 测试miniconda,python以及机器学习包是否安装成功

    1.测试安装版本 conda -V python -V 2.安装的命令 (1)库升级和安装 升级全部库:  conda upgrade --all [不知道为什么,我的conda install nu ...

  9. 学习笔记TF056:TensorFlow MNIST,数据集、分类、可视化

    MNIST(Mixed National Institute of Standards and Technology)http://yann.lecun.com/exdb/mnist/ ,入门级计算机 ...

  10. 11. IDS (Intrusion detection systems 入侵检测系统 6个)

    Snort该网络入侵检测和防御系统擅长于IP网络上的流量分析和数据包记录. 通过协议分析,内容研究和各种预处理器,Snort可以检测到数千个蠕虫,漏洞利用尝试,端口扫描和其他可疑行为. Snort使用 ...