'Why System.Windows.Automation.Peers.AutomationPeer.GetPattern() method does not use Generics? [closed]
The GetPattern() method implementation of WPF UI Automation system is implemented taking the enum parameter PatternInterface and we normally use it in the following way:
//Code with original implementation
ButtonAutomationPeer buttonPeer = new ButtonAutomationPeer(button1);
IInvokeProvider provider = (IInvokeProvider)buttonPeer.GetPattern(PatternInterface.Invoke); //Line in Question
//To invoke the click event of button we then use the following code:
provider.Invoke();
From the above code, it seems that the line with comment Line in Question is not strongly typed, we need to cast the return from GetPattern() method to the required interface and then use it to invoke the specific UI automations.
Question is:
Would it not have been better if the implementation of the GetPattern() method in WPF was done using already present Generics in .Net Framework as below:
public T GetPattern<T>;
- where, I would then pass the required interface pattern name while
calling the
GetPattern<T>method and get that interface instance strongly typed and would also not need a cast. What thought has Microsoft given in the original implementation ofGetPattern()method requiring anenum? - Would using enums in the method parameters not break the
maintainability of the
GetPattern()original implementation. I would say that when a new Control interface pattern is needed to be supported, that pattern interface's enum value would need to be added to the enum parameter namedPatternInterface
I suppose it is easier and better to call the method and get the interface pattern using the below new code that uses calling the Generic implementation:
//Code with New Generics based implementation
ButtonAutomationPeer buttonPeer = new ButtonAutomationPeer(button1);
IInvokeProvider provider = buttonPeer.GetPattern<IInvokeProvider>(); //Line in Question
//To invoke the click event of button we then use the following code:
provider.Invoke();
Solution 1:[1]
It is for the usual reason: they didn't have a time machine. Visible from the "History" annotations in the source code files available from the Reference Source, work on the UI Automation classes started around June 2003 with evidence that it got derived from earlier work. Generics didn't become available until 2005.
From dd/wpf/src/UIAutomation/UIAutomationTypes/System/Windows/AutomationPattern.cs:
// History:
// 06/02/2003 : BrendanM Ported to WCP
Which was very likely Brendan McKeon. No decent guess at what "WCP" might have meant.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hans Passant |
