C#数据注释 - 使用显示名称设置属性模型

时间:2018-04-17 10:52:23

标签: c# c#-4.0 data-annotations

我有一个名为Model的显示名称的列表:

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");

shell.GetParameter从POST获取值。

这就是我想要的: 我有一个方法来获取所有Display attr

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();
}

我的收藏是一系列字符串 ex:collection {“AccountId”,“AllocatedManagedMemory”......}

我的模型是TVSystemViewData

foreach (item in collection)
{
    if(item == modelProperty name){
         // i don know how
         model.property = shell.getParameter(item)
    }
}

[增订] 我正在使用这个:

        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循环。 还在寻找最好的方法

1 个答案:

答案 0 :(得分:0)

你需要让类继承自IEnumerator,或者自己添加一个GetEnumerator方法。

var model = new TVSystemViewData();

foreach(var item in model)
{
  item.AccountId = shell.getParameter("AccountId");
  //item.AllocatedManagedMemory ...
}

有关详情,请参阅此帖子:How to make the class as an IEnumerable in C#?

查看此文章:https://support.microsoft.com/en-us/help/322022/how-to-make-a-visual-c-class-usable-in-a-foreach-statement

// EDIT。忘了这部分:

    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;
        }
    }

编辑根据您的更新问题:我不知道这是否可行,但它应该有效。

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));
                }
            }
        }