我遇到了一个看似简单的问题,但我似乎找不到答案。我有一个带有属性的类。这些属性之一返回一个列表。我有一个方法可以循环访问任何类型的类的所有属性,并为该类(通信日志应用程序)生成一个 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?
发布评论
评论(3)
首先,检查命名空间是不够的(或者在这种情况下根本不需要)。您可以检查 prop.PropertyType 是否是 ICollection(您可以使用IsAssignableFrom)。我建议 ICollection 因为你似乎只关心计数。
然后,您可以将其转换为 ICollection(非通用)并运行 Enumerable.Cast< /a>,例如:
与直接转换为 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:
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.
你试过吗
did you try
我猜你想要逆变。
你想这样做吗?
List
C# 4.0 为接口和委托泛型参数引入了协变和逆变,但无论如何,
IList
不具有逆变。以我的拙见,我相信您需要重新考虑您的对象图!
PD:如果您需要相关指导,请发表评论!
I guess you want contravariance.
Are you trying to do so?
List<object>
andList<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!