开发者

TypeDescriptor.GetProperties vs. Type.GetProperties

开发者 https://www.devze.com 2023-04-08 15:23 出处:网络
I\'m looking at some code where an MSDNauthor uses the following in different methods of the same class:

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开发者_如何学Go?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号