Consider using reflection to optimize code paths in stock scenarios
See original GitHub issueWinForms is almost completely configurable but that comes at a performance cost. When we get windows messages (WM_*) we create event classes and jump through a lot of hoops that would not be necessary in a closed system.
For one example, getting a WM_ERASEBKGND often ends up being a FillRect on the passed in HDC. We could potentially avoid a lot of hoop jumping some allocations if we knew the OnPaintBackground was completely under our control.
We would pay up front for this sort of check, but could potentially earn it back quite quickly. The following code takes a couple of milliseconds on class creation:
Type type = GetType();
_isTypeInAssembly = type.Assembly == typeof(Control).Assembly;
_isOnPaintBackgroundLocal = _isTypeInAssembly
|| type.GetMethod(
"OnPaintBackground",
BindingFlags.NonPublic | BindingFlags.Instance,
null,
new Type[] { typeof(PaintEventArgs) }, null
).DeclaringType.Assembly == typeof(Control).Assembly;
If we find measurable situations where we go “if only we knew this wasn’t customized” this may be a strategy to attempt.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:5 (5 by maintainers)
Top Results From Across the Web
C#: Can someone explain the practicalities of reflection?
18 Answers. Reflection provides the ability to determine things and execute code at runtime. You don't have to use it if you don't...
Read more >Reflection in Native Image
At this point, code can collect information about methods and fields and store them in their own data structures, which are then reflection-free...
Read more >Reflection in Go: Use cases and tutorial
Reflection lets developers solve some problems by writing less code. However, reflection often affects the readability of your code, and it may ...
Read more >Chapter 4. Query Performance Optimization
This chapter begins with general query design considerations—the things you should consider first when a query isn't performing well. We then dig much...
Read more >Optimizing reflection in C# via dynamic code generation
A technical deep dive into C# techniques to make reflection-based code run faster using dynamic code generation and other optimizations.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

As a disclaimer, this issue come up in our recent backlog triage, but this work is not high on our todo list, and we don’t expect any immediate actions here (at least form our side).
Thank you for your thoughts. I don’t think source generators would really work in this case - they are a “compile-time thing”, and at runtime descendant types may be dynamically loaded or generated (e.g., plugins). The other two approaches in theory sound plausible, though as you said, they carry own risks and limitations. But these may be a good start in the exploration of this problem space.
We can come up with something which works like ComWrappers. Each library can be responsible for their own drawing. But so far, this is theoretical exercise. Again, seems to be overkill, given that source generators has a price.