为什么System.Windows.Automation.Peers.AutomationPeer.GetPattern()方法不使用泛型?

时间:2012-06-17 16:58:33

标签: wpf generics c#-4.0 .net-4.0 code-design

WPF UI Automation system PatternInterface方法实现是通过枚举参数//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(); 实现的,我们通常按以下方式使用它:

GetPattern()

从上面的代码中,似乎带有注释 Line in Question 的行不是强类型的,我们需要将GetPattern()方法的返回值转换为所需的接口然后使用它可以调用特定的UI自动化。

问题是: 如果WPF中的Generics方法的实现是使用.Net Framework中已存在的public T GetPattern<T>; 完成的,那会不会更好:

GetPattern<T>
  • 其中,然后我将传递所需的接口模式名称 调用GetPattern()方法并获取该接口实例 强类型并且 需要 强制转换 。什么 思想已经微软在最初的实施中给出了 enum方法需要GetPattern()
  • 在方法参数中使用枚举会不会破坏 PatternInterface原始实现的可维护性。一世 会说当需要一个新的Control接口模式时 支持,模式接口的枚举值需要 添加到名为//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();
  • 的枚举参数中

我认为使用下面使用调用Generic实现的新代码调用方法并获取接口模式更容易也更好:

{{1}}

1 个答案:

答案 0 :(得分:2)

这是通常的原因:他们没有时间机器。从参考源中可用的源代码文件中的“历史”注释中可以看到,UI自动化类的工作在2003年6月左右开始,有证据表明它来自早期的工作。仿制药直到2005年才出现。

来自dd / wpf / src / UIAutomation / UIAutomationTypes / System / Windows / AutomationPattern.cs:

// History:
//  06/02/2003 : BrendanM Ported to WCP

Brendan McKeon很可能。对“WCP”可能意味着什么没有好的猜测。