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



我有一个列表,其中包含模型的显示名称字符串:

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

我的收藏是字符串列表例如:集合 {"AccountId","AlatedManagedMemory"...}

我的模型是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 循环。仍在寻找最佳方法

您需要

使类继承自IEnumerator,或者自己添加GetEnumerator方法。

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

请参考这篇文章了解更多信息:如何在 C# 中将类设置为可枚举的 IEnumerable?

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

忘记了这部分:

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

最新更新