【问题标题】:Retrieve List of unknown type from PropertyInfo从 PropertyInfo 检索未知类型的列表
【发布时间】:2014-04-19 23:21:07
【问题描述】:

我有一个定义了列表的类。

class Program
{          
      public List<string> list { get; set; }
};

我需要使用 PropertyInfo 检索此列表,但问题是 List 可以是任何类型(此处为字符串)。如何使用 propertyinfo 提取列表的值,因为 getValue() 不会返回列表。

【问题讨论】:

    标签: c# propertyinfo


    【解决方案1】:

    您可以将其转换为IList,然后使用循环:

    var myList = (IList)typeof(Program)
                        .GetProperty("list")
                        .GetValue(yourInstance);
    

    如果你不知道你的列表的实际类型,你能做的就是这些。如果你在编译时知道类型,那么你可以把它转换成IList&lt;T&gt; p>

    【讨论】:

    • 没有帮助...转换为 IList 显示错误“使用泛型类型 'System.Collections.Generic.IList' 需要 1 个类型参数”
    • 你需要using System.Collections;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2017-05-28
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多