Any static analysis of this kind would be very difficult to implement correctly. You can use heuristics (e.g., UIElement
), but you'll probably end up with some false positives and/or false negatives.
For example, FlowDocument
does not derive from UIElement
. You could change your heuristic to test for DispatcherObject
-derived types, but then that would also include Freezable
, which may or may not require context - you can't always know at compile-time. So that's a guaranteed false positive (or negative) in the general case.
As another example, adding items to a collection that is exposed as a data-bound property would also require the context, even though the collection is not a UI element.
Similar problems exist in ASP.NET. HttpContext.Current
is obvious, but what about string formatting methods that implicitly use the current culture? There's a number of "gotchas" on the ASP.NET side, too.
That said, I do think it's a good idea; just be sure to have an easy way to ignore false positives and false negatives.