Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top