【问题标题】:Can I set multiple List Item property values with Reflection?我可以使用反射设置多个列表项属性值吗?
【发布时间】:2021-04-09 09:52:46
【问题描述】:

继续:Can I set a property value with Reflection?,我有一个挑战-

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

列表选择有 2 个项目 1.

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

如何将 Selections 分配给 Student 类的 Courses 属性?

【问题讨论】:

  • 什么是列表选择?您是否需要将其中一项转换为ListItem 并将其添加到Courses 列表中?为什么你认为你需要反射?
  • ‘Selections’是包含上述两个ListItem的List的名称
  • 我想将 Selected = true 的 ListItem 添加到 Courses。
  • 我需要反思的原因是因为像学生类一样,有很多类,并且根据这一点,我想查找 property.name 并分配值。现在,我可以为文本框做到这一点。我也希望能够使用 ListBox 来做到这一点
  • 是的,但这不是问题所在。 什么是选择 询问列表包含哪些类型的项目。是Selections&lt;ListItem&gt;吗? Courses 的相同 ListItem 对象?或者是别的什么?您可以发布这些列表包含的对象的类定义吗?

标签: c# reflection propertyinfo


【解决方案1】:

为了回答这个问题,我发现 .net 会自动将属性标识为 List,并且我已经能够使用 property.SetValue() 方法循环并分配值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多