TypeDescriptor.GetProperties vs. Type.GetProperties

╄→гoц情女王★ 提交于 2020-01-01 05:41:46

问题


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?


回答1:


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.

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.



来源:https://stackoverflow.com/questions/7573913/typedescriptor-getproperties-vs-type-getproperties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!