问题描述
我期待在那里的MSDN笔者使用在同一类的不同方法,下面的代码:
I'm looking at some code where an MSDN author uses the following in different methods of the same class:
if ( TypeDescriptor.GetProperties(ModelInstance)[propertyName] != null ) return;
var property = ModelInstance.GetType().GetProperty(propertyName);
你会使用前者,因为它的速度更快,你只需要查询一个属性,而后者,如果你需要处理的呢?别的东西吗?
Would you use the former because its faster and you only need to query a property and the latter if you need to manipulate it? Something else?
推荐答案
第一种方法通常不应更快,因为每默认内部它实际上使用第二种方法。该TypeDescriptor架构的正常反射(其中 instance.GetType()的getProperty(...)
表示之上增加功能见的有关TypeDescriptor架构的更多信息。
The first method should generally not be faster since internally per default it actually uses the second method. The TypeDescriptor architecture adds functionality on top of the normal reflection (which instance.GetType().GetProperty(...)
represents. See http://msdn.microsoft.com/en-us/library/ms171819.aspx for more information about the TypeDescriptor architecture.
在一般使用反射直接更快(以上即你的第二行),但也有可能是,如果一些自定义类型提供是使用TypeDescriptor中使用的一个原因可能返回其它结果比标准的反射。
In general using reflection directly is faster (i.e. your second line above), but there may be a reason for using the TypeDescriptor if some custom type provider is in use which may return other results than the standard reflection.
这篇关于TypeDescriptor.GetProperties与Type.GetProperties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!