【发布时间】:2020-10-15 08:12:57
【问题描述】:
我定义了一个配置,用于对下面模型中的每个属性进行重复检查
public class ConfigurationModel
{
public GroupValue Value1 { get; set; }
public GroupValue Value2 { get; set; }
public GroupValue Value3 { get; set; }
public GroupValue Value4 { get; set; }
public GroupValue Value5 { get; set; }
}
public class GroupValue
{
public bool duplicateCheck { get; set; }
}
我有一个真正的类,它具有定义的所有属性,如 Value1、Value2 等,我们将其称为 OrderModel
public class OrderModel
{
public string Value1 { get; set; }
public string Value2 { get; set; }
public string Value3 { get; set; }
public string Value4 { get; set; }
public string Value5 { get; set; }
}
现在我只想包含那些在配置中将相应标志设置为 true 的属性以进行重复检查。示例
bool CheckDuplicate(OrderModel newOrder)
{
OrderModel existingOrder = //GetfromDB;
//Compare only those properties in newOrder and existingOrder which have duplicate check set to true in ConfigurationModel
}
使用反射通过字符串名称获取属性然后比较是我的想法,但想检查最好的方法,同时牢记性能。任何帮助将不胜感激。
谢谢, AK
【问题讨论】:
标签: c# reflection dynamic-attributes