.NET Roslyn Analyzers to Detect and Fix common pitfalls in Windows Development #4562
Unanswered
michael-hawker
asked this question in
Ideas
Replies: 1 comment 1 reply
-
For this one, we should likely fix CsWin32 first, since the code it generates isn't AOT-compatible. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Windows development can be difficult. Especially for some things which will compile and seemingly maybe run fine, but then actually not be working correctly.
Scenarios / Example
For instance, with Dependency Properties there's a lot of implicit naming requirements and other traps which can cause unintended behavior or cause things not to work without a compiler error. This wastes a lot of development time chasing down things which are essentially known patterns.
For a Dependency Property example, the worst instance of this I've seen a number of times is the initialization of a list in the static PropertyMetadata instead of the class constructor. The existing XAML errors lead you down that anti-pattern, so an analyzer here that encodes the remarks of the DependencyProperty docs would be much more helpful.
Solution
Fortunately, .NET has been making a lot of improvements and investments in Roslyn Analyzers and CodeFixers. These can detect patterns of code and offer solutions to fix them.
For instance, if you copy a DependencyProperty from one class to another, it won't work if you don't update the owner type to match the containing class. This'll compile fine as both classes still exist, but we can detect this mismatch with an analyzer and offer to automatically fix it for the developer so they learn of their mistake before they even try to run their code:
DependencyPropertyOwnerTypeFixer.mp4
Prototype and Ideas
On that note, I've been learning and experimenting with some of these scenarios (starting with Dependency Properties because you know, I'm the XAML Llama... 😋) and put that work in a repository here to continue experimenting and collecting potential scenarios that are commonly encountered.
There can be many other patterns and fixes available for other developers as well. For instance:
It may not be possible to provide the best code fix for every scenario, but flagging potential issues with warnings or errors is half the battle. Just a simple code fix suggestion can help common scenarios and otherwise help inform a developer of potential solutions that they can then modify or extend for their specific scenario.
Once understanding how to create these, I'd guess an analyzer/code-fixer would take a couple of hours to write and test? It seems best to break them down into smaller pieces, like there's many DependencyProperty ones but they can work together to guide folks down the right path.
https://github.com/michael-hawker/WindowsAnalyzers
Related Issues
Beta Was this translation helpful? Give feedback.
All reactions