C# 反射获取属性值、名称、类型以及集合的属性值、类型名称

时间:2021-05-09 09:03:45

实体类

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