比较清单. PropertyInfo.GetType()的集合 [英] COmparing List. collection with PropertyInfo.GetType()

查看:122
本文介绍了比较清单. PropertyInfo.GetType()的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下循环遍历属性集合:

I am looping through property collection using below loop:

foreach (System.Reflection.PropertyInfo eachProperty in this.UIPageAttributes.GetType().GetProperties())
{

}



现在,对于我想要的每个属性,将其与List<>进行比较.收藏.用于检查任何其他数据类型,甚至检查我使用的任何实体:



Now for eachProperty i want to ,compare it against the List<> collection. For checking against any other data type and even for any entity i m using:

if (eachProperty.GetType().Equals(typeof(string))

  if(eachProperty.GetType().Equals(typeof(EntityClass)))
;;;;



但是为了检查List<> ;,收集类型,我没有找到可以直接检查此类型的任何内容,

任何帮助都将非常有用...



But for checking List<> collection type, I didnt find anything where I can directly check for this type,

Any help would be great...

推荐答案

首先eachProperty的类型为System.Reflection.PropertyInfo,而不是其描述的类型.因此,eachProperty.GetType()将始终返回typeof(System.Reflection.PropertyInfo).此元数据描述的类型为eachProperty.PropertyType.用它来解决您的问题.

一些小问题是使用object.Equals进行类型比较.它不是很好,甚至性能很差,因为它的实现可能需要动态检查类型(尽管您可能并不在意,但仍然如此).而是通过"=="运算符使用比较,该运算符仅与System.Type类型一起使用,并且也更易读.

所有这些活动的最终目标尚不清楚(您没有解释,这是一个常见错误),对我来说似乎有些可疑.每次对反射类型进行显式比较时,表明可能违反OOP,这是对实际OOP设计的替代.我不能根据您的信息确定地说,所以也许就考虑一下.

-SA
First of all eachProperty has the type System.Reflection.PropertyInfo, not the type it describes. So, eachProperty.GetType() will always return typeof(System.Reflection.PropertyInfo). The type described by this meta-data is eachProperty.PropertyType. Use it to solve your problem.

Some minor problem is using object.Equals for type comparison. It''s not good and is even of low performance because its implementation may need to check up type dynamically (you may not care about it, but nevertheless). Instead, use comparison via "==" operator which only works with the type of System.Type and is also more readable.

The ultimate goal of all this activity is not clear (you did not explain it, which is a common mistake) and looks somewhat suspicious to me. Every time the reflected types are explicitly compared suggests it could be OOP violation, a substitution of real OOP design. I cannot say for sure based on your information, so perhaps just think about it.

—SA


foreach (System.Reflection.PropertyInfo eachProperty in this.UIPageAttributes.GetType().GetProperties())
{
if (eachProperty.PropertyType == this.EmployeeObject.DepartmentList.GetType())  


解决了我的问题.

感谢您的建议.


Solved my problem.

Thanks for suggestions.


您必须在EntityClass类中实现iComparable接口.
并且必须在类内部实现equals 方法,则必须编写自己的逻辑来检查两个对象的相等性.

请点击链接以了解更多信息..

http://support.microsoft.com/kb/320727 [
you have to implement iComparable Interface in your EntityClass class.

and you have to implement the equals method inside your class, you have to write your own logic to check the equality of two objects.

follow the link to learn more..

http://support.microsoft.com/kb/320727[^]


mark as answer if solves your problem, it motivates :)


这篇关于比较清单. PropertyInfo.GetType()的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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