C#数据注释-使用显示名称设置属性模型
我有一个包含模型显示名称字符串的列表:C#数据注释-使用显示名称设置属性模型,c#,c#-4.0,data-annotations,C#,C# 4.0,Data Annotations,我有一个包含模型显示名称字符串的列表: public class TVSystemViewData : BaseViewData { [Display(Name = "AccountId", Description = "")] public String AccountId { get; set; } [Display(Name = "AllocatedManagedMemory", Description = "")]
public class TVSystemViewData : BaseViewData
{
[Display(Name = "AccountId", Description = "")]
public String AccountId { get; set; }
[Display(Name = "AllocatedManagedMemory", Description = "")]
public String AllocatedManagedMemory { get; set; }
[Display(Name = "AllocatedPhysicalMemory", Description = "")]
public String AllocatedPhysicalMemory { get; set; }
[Display(Name = "AudioMute", Description = "")]
public String AudioMute { get; set; }
}
我需要使用foreach循环设置属性,以便将值添加到我的模型中:
这就是如何从POST应用程序中获取值
var model.AccountId = shell.getParameter("AccountId")
var model.AllocatedManagedMemory = shell.getParameter("AllocatedManagedMemory");
GetParameter从POST获取值
这就是我想要的:
我有一个获取所有显示属性的方法
public List<String> GetTVSystemProperties()
{
return typeof(TVSystemViewData)
.GetProperties()
.SelectMany(x => x.GetCustomAttributes(typeof(DisplayAttribute), true) //select many because can have multiple attributes
.Select(e => ((DisplayAttribute)e))) //change type from generic attribute to DisplayAttribute
.Where(x => x != null).Select(x => x.Name) //select not null and take only name
.ToList();
}
[更新]
我用的是:
foreach (var property in UtilsHandler.getConfigAsList("sysDataSource"))
{
//Set Values to Model
try
{
model.GetType().GetProperty(property).SetValue(model, shell.getParameter(property), null);
}
catch (Exception)
{
Have issue with data types
}
}
我对数据类型有问题。
但我使用一个foreach循环。
仍在寻找最佳方法您需要使类从IEnumerator继承,或者自己添加GetEnumerator方法
var model = new TVSystemViewData();
foreach(var item in model)
{
item.AccountId = shell.getParameter("AccountId");
//item.AllocatedManagedMemory ...
}
有关更多信息,请参阅本帖:
请查看这篇文章:
//编辑。忘记这一部分:
List<TVSystemViewData> model;
[Display(Name = "AccountId", Description = "")]
public String AccountId { get; set; }
[Display(Name = "AllocatedManagedMemory", Description = "")]
public String AllocatedManagedMemory { get; set; }
[Display(Name = "AllocatedPhysicalMemory", Description = "")]
public String AllocatedPhysicalMemory { get; set; }
[Display(Name = "AudioMute", Description = "")]
public String AudioMute { get; set; }
public IEnumerator<TVSystemViewData> GetEnumerator()
{
foreach (var item in model)
{
yield return item;
}
}
列表模型;
[显示(Name=“AccountId”,Description=”“)]
公共字符串AccountId{get;set;}
[显示(Name=“AllocatedManagedMemory”,Description=”“)]
公共字符串AllocatedManagedMemory{get;set;}
[显示(Name=“AllocatedPhysicalMemory”,Description=”“)]
公共字符串AllocatedPhysicalMemory{get;set;}
[显示(Name=“AudioMute”,Description=”“)]
公共字符串AudioMute{get;set;}
公共IEnumerator GetEnumerator()
{
foreach(模型中的var项目)
{
收益回报项目;
}
}
根据您的更新问题进行编辑:我不知道这是否是一种方式,但它应该有效
var model = new TVSystemViewData();
PropertyInfo[] properties = typeof(TVSystemViewData).GetProperties();
List<string> items = new List<string> { "AccountId", "AllocatedManagedMemory" }; //your collection of strings
foreach (var item in items)
{
foreach (var property in properties)
{
if (item == property.Name)
{
property.SetValue(model, shell.getParameter(item));
}
}
}
var模型=新的TVSystemViewData();
PropertyInfo[]properties=typeof(TVSystemViewData).GetProperties();
列表项=新列表{“AccountId”,“AllocatedManagedMemory”}//你的字符串集合
foreach(项目中的var项目)
{
foreach(属性中的var属性)
{
if(item==property.Name)
{
SetValue(model,shell.getParameter(item));
}
}
}
我更新了问题的更多细节。在您的回答中,我必须始终使用model.AccountId=some;模型。音频静音=一些;我已经更新了我的答案,希望能有所帮助。至少可以为您指出正确的方向,也是一个类似的主题,如果这没有帮助,请查看:我使用我使用的代码进行更新。删除一个foreach。您应该添加正在获取的错误。我试过你的最新产品,效果很好。shell.gerParameter(项)是否返回字符串?我想,但是“UtilsHandler.getConfigAsList(“sysDataSource”)是否为您提供了正确的数据类型字符串?为什么要迭代它而不是你提到的字符串列表?shell总是返回一个字符串。你可以用try-catch围住。或者,您可以使用数据类型进行切换,然后执行tryparse。。。这只是一个想法,我不确定我是否理解这个问题。您正在编写自定义模型活页夹吗?
var model = new TVSystemViewData();
PropertyInfo[] properties = typeof(TVSystemViewData).GetProperties();
List<string> items = new List<string> { "AccountId", "AllocatedManagedMemory" }; //your collection of strings
foreach (var item in items)
{
foreach (var property in properties)
{
if (item == property.Name)
{
property.SetValue(model, shell.getParameter(item));
}
}
}