Take the 2-minute tour ×

I need to dynamically set values on a bunch or properties on an object , call it a transmission object. There will be a fair number of these transmission objects that will be created and have its properties set in a short space of time.I want to avoid the use of reflection, are there alternativ? If so are there sample implementations I could look at?


share|improve this question
Any reason why you want to avoid reflection? –  Otávio Décio Jun 22 '09 at 15:59

6 Answers 6

up vote 13 down vote accepted

Use Delegate.CreateDelegate to turn a MethodInfo into a strongly-typed delegate. This can improve performance massively. I have a blog post about this with sample code. Note that this is only going to help if you need to set the same properties multiple times - basically it means that a lot of the type checking is done once when you create the delegate, rather than on every invocation.

Marc Gravell has a HyperPropertyDescriptor project which achieves even better performance, but introduces an extra dependency.

share|improve this answer
Got it thanks msmvps.com/blogs/jon_skeet/archive/2008/08/09/… –  AndyMM Jun 22 '09 at 16:13
That's so much better then trying to make a Dynamic method –  JoshBerke Jun 22 '09 at 16:34
I've got some interesting examples of this in .NET 4.0 with DLR trees - hopefully for the article ;-p –  Marc Gravell Jun 22 '09 at 19:30

Reflection can be blazingly fast if you do it right (not as fast as static code, of course).

Finding a property-setter is slow. Invoking a delegate is fast.

You need to get and cache Delegate objects for each property-setter on each type of DTO. That's the slow part, but it's a one-time hit. Then you can Invoke each of the cached delegates for the property-setters of a given DTO type, passing in the DTO object and the new property value, but this part will be very fast.

share|improve this answer
Thanks Justice I will try your idea. –  AndyMM Jun 22 '09 at 16:22

In .NET 4.0 (beta), you can do this with the updated expression trees, using Expression.Block and Expression.Assign - then compile that to a typed delegate; job done.

In .NET 2.0 and above (as Jon mentioned) HyperDescriptor is a reasonable option - it works as a custom PropertyDescriptor implementation, so you just do code like:

// store this collection for optimum performance
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(
props["Name"].SetValue(obj, newName);
props["DateOfBirth"].SetValue(obj, newDoB);

This still has a little boxing, but that isn't actually a bottleneck.

share|improve this answer

Have you established with certainty that using reflection is too slow? Although reflection in .NET is not as fast as static code, it is still extremely fast. You should write the code in the easiest way possible - even if that uses reflection - and only come back to optimize if you notice performance problems and isolate them to your use of reflection. Most of the time, you won't have any problems. Reflection is used in all kinds of performance-sensitive code, such as ASP.NET MVC.

share|improve this answer
I guess what I am looking for is the fastest possible implementation. I am also looking at alternatives simply for due diligence. I do not want to be hamstrung into one technique , simply because it is the one I am familiar with, without exploring alternatives. –  AndyMM Jun 22 '09 at 16:04

Reflection got a bad rep from Java where it is (or at least used to be) very slow. This is not the case in .net so I don't understand your objection on using it. Also I agree with Rex, you can't say something has poor performance without actually measuring.

share|improve this answer
I did not say it had poor performance , I implied it ! jokes aside please see comment to rex. –  AndyMM Jun 22 '09 at 16:06
It depends what you mean by "very slow". It can very easily still be a bottleneck if you use it naively... but with a bit of work (e.g. Delegate.CreateDelegate and the like) it can be made pretty nippy. –  Jon Skeet Jun 22 '09 at 16:27

Your Answer


