C# 有没有办法检查模型中某个属性的数量?
我有以下型号:C# 有没有办法检查模型中某个属性的数量?,c#,.net,C#,.net,我有以下型号: [Key] public string Id { get; set; } [IsSearchable] public string Code{ get; set; } [IsSearchable] public string Name { get; set; } [IsSearchable] public string Address { get; set; } [IsSearchable] public string PostCode {
[Key]
public string Id { get; set; }
[IsSearchable]
public string Code{ get; set; }
[IsSearchable]
public string Name { get; set; }
[IsSearchable]
public string Address { get; set; }
[IsSearchable]
public string PostCode { get; set; }
[IsFilterable]
public int? Setting{ get; set; }
[IsFilterable, IsSortable]
public Location Location { get; set; }
我正在将数据库中的模型与上面的模型进行比较,并希望检查数据库模型是否具有相同的属性。有没有办法不必硬编码就能找出属性的具体数量?例如,这是我编写用来检查的方法:
private bool CheckAttributeEquality(List<PropertyInfo>searchableAttributeProperties,
List<PropertyInfo> filterableAttributeProperties,
List<PropertyInfo> sortableAttributeProperties,
List<PropertyInfo> keyAttributeProperties)
{
if (searchableAttributeProperties.Count == 4 &&
filterableAttributeProperties.Count == 2 &&
sortableAttributeProperties.Count == 1 &&
keyAttributeProperties.Count == 1)
{
return true;
}
return false;
}
private bool checkAttributeQuality(ListsearchableAttributeProperties,
列出filterableAttributeProperties,
列出可排序属性属性,
列表键属性(属性)
{
如果(searchableAttributeProperties.Count==4&&
filterableAttributeProperties.Count==2&&
sortableAttributeProperties.Count==1&&
keyAttributeProperties.Count==1)
{
返回true;
}
返回false;
}
然而,我不想硬编码这些,我想确保它们匹配我的模型上的属性?有人能帮忙吗?什么是“数据库中的模型”?我猜您只想枚举属性,并为每个属性枚举其属性。请看。这不是您要问的,但可能会有帮助-,您可以从db模型生成模型类。什么是“数据库中的模型”?我猜您只想枚举属性,并为每个属性枚举其属性。看,这不是你要问的,但它可能会有帮助-,你可以从数据库模型生成模型类。