Background
I am currently trying to fix some behavior where Double Buffering causes a crash if a control is too large to allocate a buffer.
After much discussion and trial-and-error, we decided that the best course of action would be to enable Double Buffering for controls if the size will support it (say less than 3000x3000 pixels), but if it exceeds that size threshold, then we will programmatically turn off double buffering.
This turns out to be rather annoying to do, as the GetStyle and SetStyle methods are protected, and there are many 3rd party controls that we need to disable double buffering for. To overcome this, I created a helper method that disables Double Buffering via reflection, which seemed like the lesser of all evils. I then monitor the size of controls via the Layout event, and set the DoubleBuffer and OptimizedDoubleBuffer flags accordingly.
Question
I'm not overly familiar with reflection, and my main question is what sort of performance impact should I expect from getting/setting ControlStyles via the MethodInfo.Invoke method.
In particular, I'm wondering if it would be any help to guard against setting the DoubleBuffer/OptimizedDoubleBuffer ControlStyles by first reading in the value (which would also have to be done using GetStyle() via reflection). Or is it basically the same to just call SetStyle() each time. I'm concerned because the Layout event is called a lot, and I wouldn't want to severely degrade performance just for this double buffering tracking logic.
Any input would be appreciated, in particular about the performance implications of what I'm doing, but also if anyone has a better idea for disabling double buffering for controls only when exceeding a certain size.
I recommend that you read the style first to see if you even need to call SetStyle. Setting the style is likely to be expensive.
A small count of reflection access is really nothing compared to allocating a double buffer and drawing. Don't think about it.