0

In continuation to: Can I set a property value with Reflection?, I have a challenge-

class Student
{
    public string Name { get; set; }
    public List<ListItem> Courses { get; set; }
}

List Selections has 2 Items 1.

{
Text = 'Business Management', Value = 'Course 1', Selected = 'true'
}
{
Text = 'Network Management', Value = 'Course 2', Selected = 'false'
}

How do I assign the Selections to Courses property of the Student class?

vishwa
  • 1
  • What is *List Selections*? Do you need to convert one of those Items to a `ListItem` and add it to the `Courses` List? Why do you think you need Reflection for this? – Jimi Apr 09 '21 at 10:53
  • ‘Selections’ is the name of the List containing the above two ListItems – vishwa Apr 10 '21 at 15:54
  • I want to add the ListItem with Selected = true to Courses. – vishwa Apr 10 '21 at 16:00
  • The reason I need reflection is because like Student class, there are a good number of classes and depending on this, I would like to look for property.name and assign value. Right now, I am able to do it for text boxes. I want to be able to do it with a ListBox as well – vishwa Apr 10 '21 at 16:02
  • Yes, but that wasn't the question. *What is Selection* is asking what kind of items that list contains. Is it `Selections`? The same `ListItem` object of `Courses`? Or is it something else? Can you post the class definitions of the objects that these lists contain? – Jimi Apr 10 '21 at 18:24
  • List Selections = new List(); using (Entities DB = new Entities){ Selections = DB.StudentSelections.Select(x => new ListItem{ Text = x.CourseName , Value = x.CourseId, Selected = x.OptInStatus }) } – vishwa Apr 12 '21 at 00:52
  • This is how I’m extracting the List details from the database. The object type is simply a list of ListItem as per the above —Vishwa – vishwa Apr 12 '21 at 00:55

1 Answers1

0

To answer to this, I have found that .net automatically identifies the Property as List and I have been able to loop through and assign values using the property.SetValue() method

vishwa
  • 1