转换 C# 列表<>

发布于 2024-10-22 12:39:09 字数 635 浏览 2 评论 0 原文

我遇到了一个看似简单的问题,但我似乎找不到答案。我有一个带有属性的类。这些属性之一返回一个列表。我有一个方法可以循环访问任何类型的类的所有属性,并为该类(通信日志应用程序)生成一个 TreeNode。当我遇到标识为列表的属性时,我不知道如何正确转换 property.GetValue 。 property.PropertyType 是已知的,但无论我尝试什么,我都会收到编译错误或运行时错误。

这就是我想要做的...

foreach (PropertyInfo prop in props)
{
    if(prop.PropertyType.Namespace == "System.Collections.Generic")
    {
        List<object> oList = prop.GetValue(data, null);
        MessageBox.Show(oList.Count.ToString())
    }
}

如果我在 GetValue 行上放置一个断点,prop 参数就知道它是一个包含三个元素的“myclass”项的列表。我只是无法将其转换为对象列表(这很好)或将其转换为实际的“myclass”元素列表,这会更好。如何将 PropertyInfo.GetValue(对象)的返回值转换为其列表?

I have what seems to be a simple problem but one that I can't seem to find answers to. I have a class with properties. One of those properties returns a List. I have a method that cycles through all properties of any kind of class and produces a TreeNode for that class (a communication log application). When I come across the property identified as a List, I don't know how to cast the property.GetValue properly. the property.PropertyType is known but what ever I try, I get a compilation error or a runtime error.

Here is what I'm trying to do...

foreach (PropertyInfo prop in props)
{
    if(prop.PropertyType.Namespace == "System.Collections.Generic")
    {
        List<object> oList = prop.GetValue(data, null);
        MessageBox.Show(oList.Count.ToString())
    }
}

If I put a breakpoint on the GetValue line, the prop parameter knows that it's a list of "myclass" items with three elements. I just can't cast it to either a list of objects (which would be fine) or cast it to a list of actual "myclass" elements which would be even better. How do I cast the return value of PropertyInfo.GetValue (an object) to its List?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小兔几 2024-10-29 12:39:09

首先,检查命名空间是不够的(或者在这种情况下根本不需要)。您可以检查 prop.PropertyType 是否是 ICollection(您可以使用IsAssignableFrom)。我建议 ICollection 因为你似乎只关心计数。

然后,您可以将其转换为 ICollection(非通用)并运行 Enumerable.Cast< /a>,例如:

IEnumerable<MyClass> res = ((ICollection)prop.GetValue(data,null)).Cast<MyClass>();
MessageBox.Show(res.Count().ToString());

与直接转换为 List 相比的优点是这适用于任何集合。但如果您不需要,您可以尝试其他答案的建议。

First of all, it's not enough (or needed in this case at all) to check the namespace. You can check if prop.PropertyType is an instance of ICollection (you can use IsAssignableFrom). I'm suggesting ICollection because you only seem to care about the Count.

You can then cast it to an ICollection (non-generic) and run Enumerable.Cast, like:

IEnumerable<MyClass> res = ((ICollection)prop.GetValue(data,null)).Cast<MyClass>();
MessageBox.Show(res.Count().ToString());

The advantage over converting directly to List is that this will work with any collection. But if you don't need that, you can try what the other answer suggests.

无语# 2024-10-29 12:39:09

你试过吗

prop.GetValue(data, null) as List<YourClass>;

did you try

prop.GetValue(data, null) as List<YourClass>;
夜还是长夜 2024-10-29 12:39:09

我猜你想要逆变。

你想这样做吗?

List<object> list = (List<Product>)object;

ListList 不会被视为同一类型。

C# 4.0 为接口和委托泛型参数引入了协变和逆变,但无论如何,IList 不具有逆变。

以我的拙见,我相信您需要重新考虑您的对象图!

PD:如果您需要相关指导,请发表评论!

I guess you want contravariance.

Are you trying to do so?

List<object> list = (List<Product>)object;

List<object> and List<Product> wouldn't be treated as the same type.

C# 4.0 introduced covariance and contravariance for interface and delegate generic parameters, but anyway, IList<T> doesn't have contravariance.

In my humild opinion, I believe you'll need to reconsider your object graph!

PD: Just comment out if you want guidance for that!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文