0

I have a list with Strings of Display names of 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; }
}

I need to set the properties with a foreach loop to add the values to my Model:

This is how get values from POST the application

var model.AccountId = shell.getParameter("AccountId")
var model.AllocatedManagedMemory = shell.getParameter("AllocatedManagedMemory");

The shell.GetParameter get the value from a POST.

this is how i want: I have a a Method to get all 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();
}

My collection is a list of Strings ex: collection {"AccountId","AllocatedManagedMemory"...}

My model is TVSystemViewData

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

[UPDATED] I am using this:

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

I have issues with data types. But i use one foreach loop. Still looking for a best method

Fabio Santos
  • 253
  • 1
  • 4
  • 15

1 Answers1

0

you need to make the class inherit from IEnumerator, or add a GetEnumerator method yourself.

var model = new TVSystemViewData();

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

Please refer to this post for more information : How to make the class as an IEnumerable in C#?

Check this article out: https://support.microsoft.com/en-us/help/322022/how-to-make-a-visual-c-class-usable-in-a-foreach-statement

//EDIT. Forgot this part :

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

EDIT According to your update question: I don't know if this is the way to go but it should work.

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));
                }
            }
        }
bzLoLIp0p
  • 91
  • 4
  • I Update the question with more detail. In your Answer i have to do always model.AccountId = some; model.AudioMute=some; I have 68 properties to model – Fabio Santos Apr 18 '18 at 10:38
  • I have updated my answer, hope it helps. At least maybe point you in the right direction, also a similar topic, check it out if this doesn't help : https://stackoverflow.com/questions/8151888/c-sharp-iterate-through-class-properties – bzLoLIp0p Apr 18 '18 at 11:25
  • I update with the code i use. remove one of your foreach. – Fabio Santos Apr 18 '18 at 13:51
  • You should add the error that you are getitng. I tried your updated thing and works fine. Does shell.gerParameter(item) return you a string? I suppose, but does the "UtilsHandler.getConfigAsList("sysDataSource")" give you the correct data type string? Why do you iterate that instead of your list of strings you mentioned? – bzLoLIp0p Apr 19 '18 at 07:14
  • the shell return always a string. you can surround with try catch. Or you can make a switch with datatypes and do a tryparse... this is just a ideia – Fabio Santos Apr 19 '18 at 09:42