如何理解类属性是List的东西 [英] How to understand class property is List of something

查看:83
本文介绍了如何理解类属性是List的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个具有基本类型属性的类,并且还有像List<>这样的类型。这个代码:

Hi everyone,
I have a class which has primitive type properties and also has types like List<> of something.
At this code:

string[] colums = typeof(T).GetProperties().Select(c => c.Name).ToArray();
foreach (var col in colums)
{
 var tc=  typeof(C).GetProperties().Select(c => c.Name).ToArray();
 var tp = col.GetType();
 var value = typeof(T).GetProperty(col).GetValue(item, null) ?? string.Empty;

 

}



我可以得到像这样的值。但是当它是List<>我不能。有什么方法可以理解属性是List<>什么?


I can get the values like this.But when it is List<> i cant.Is there any way to understand if property is List<> of something?

推荐答案

见这里: http://stackoverflow.com/questions/1503976/determine-if-property-is-generic-listof-t-via-reflection-and-loop -list-items [ ^ ]

大约一半是一个完整的控制台应用程序,可以做你想要的。
See here: http://stackoverflow.com/questions/1503976/determine-if-property-is-generic-listof-t-via-reflection-and-loop-list-items[^]
About half way down is a complete console app which should do what you want.


你可以获得以下类型的权利:





you can get type of propety with:


System.Reflection.PropertyInfo propertyInfo = typeof(ObjectName).GetProperty("propertyName");
Type type = propertyInfo .PropertyType;


这篇关于如何理解类属性是List的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆