编码的UI测试向下投射不工作

ste*_*yan 5 c# automation coded-ui-tests

我一直在尝试将泛型引入我们的自动化代码,所以我试图将WpfControl转换为泛型类型T,其中T被约束为WpfControl类型.

return control as T;
Run Code Online (Sandbox Code Playgroud)

T将始终是WpfControl的子代,但是当我将类型为WpfControl的控件转换为泛型类型时,它返回null.我还尝试将控制转换为确切的子类型,例如:

var childControl = control as WpfTabList;
Run Code Online (Sandbox Code Playgroud)

这仍然会回来null.有人遇到过类似的问题吗?

请参阅此WpfControl类

Yau*_*sau 0

一些建议:

首先,在您的通用方法中,请使用 geteric 类型规范。喜欢

public T GetControl<T>(...) where T : WpfControl
Run Code Online (Sandbox Code Playgroud)

您的假设和实现存在不一致,您尝试将 aWpfControl与 a匹配WpfTabList,在子控件不是 TabList 的情况下这是错误的。