实体类

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. java中字符串"1999-10-01T00:00:00+08: 00" 转化为Date格式

    String oldStr = "1999-10-01T00:00:00+08: 00": SimpleDateFORMAT sdf = new SimpleDateFORMAT ...

  2. 热更新-----为何使用lua进行热更

    事实上我们在安卓端是可以使用c#jit的,但是我们在ios上的代码是AOT(预先编译,静态编译)的,不能用c# jit(实时编译,即时编译). ios不能用c#热更是因为启动了CPU的No eXecu ...

  3. Python练习十一

    1.写一个程序,提示输入整数X,然后计算从1到X连续整数的和. num = int(input('please the input number:')) sum_num = 0 for i in ra ...

  4. 基于Linux-3.9.4内核的GDB跟踪系统调用实验

    382 + 原创作品转载请注明出处 + https://github.com/mengning/linuxkernel/ 一.实验环境 win10 -> VMware -> Ubuntu1 ...

  5. ScreenPresso注册码

    [3]-[screenpressopro]-[5705]-[www.dayanzai.me]-[01/26/2016]-[Zkj8i42HhuCW1UCNtaklHv7Eekr1Wkt4wKHFket ...

  6. Go - WaitGroup

    package main import ( "fmt" "sync" ) //WaitGroup用于等待一组线程的结束.父线程调用Add方法来设定应等待的线程的 ...

  7. 研究大华3G设备接入自主视频开发平台

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/fengshuiyue/article/details/24311977 近期项目上须要接入3G摄像头 ...

  8. DataFrame对行列的基本操作实战

    1.pandas对行列的基本操作命令: import numpy as np import pandas as pd from pandas import Sereis, DataFrame ser ...

  9. DNS 负载均衡

    相关文章: 文章 网址 一个域名可以绑定多个IP吗?由此引发的调查 https://ask.zkbhj.com/?/article/139

  10. DevExpress中GridControl的使用笔记(转)

    转自:https://www.jianshu.com/p/badc1d2f0841 注:练习例子为: DxApplication1 -> XtraForm1 , 例子上传到本博中 2019.4. ...