解析 XML 函数名称并在整个程序集中调用
我编写了一个通过互联网浏览器对我们的硬件进行单元测试的应用程序。
我在程序集中有命令类,它们是各个 Web 浏览器操作的包装器,例如勾选复选框、从下拉框中进行选择:
BasicConfigurationCommands
EventConfigurationCommands
StabilizationCommands
以及一组测试类 ,使用命令类执行脚本化测试:
ConfigurationTests
StabilizationTests
然后通过 GUI 调用这些命令来由我们的 QA 团队运行规定的测试。然而,由于固件在各个版本之间变化得非常快,如果开发人员能够编写一个可以调用测试或命令的 XML 文件,那就太好了:
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
<StabilizationTests>
<StressTest repetition="10" />
</StabilizationTests>
<BasicConfigurationCommands>
<SelectConfig number="2" />
<ChangeConfigProperties name="Weeeeee" timeOut="15000" delay="1000"/>
<ApplyConfig />
</BasicConfigurationCommands>
</testsuite>
我有一直在研究 System.Reflection
类,并看到使用 GetMethod
和 Invoke
的示例。这要求我在编译时创建类对象,并且我想在运行时完成所有这些。
我需要扫描整个程序集以查找类名,然后扫描类中的方法。
这似乎是一个很大的解决方案,因此任何指向我(以及这篇文章的未来读者)找到答案的信息都会很棒!
感谢您的阅读,
马特
I have written an application that unit tests our hardware via a internet browser.
I have command classes in the assembly that are a wrapper around individual web browser actions such as ticking a checkbox, selecting from a dropdown box as such:
BasicConfigurationCommands
EventConfigurationCommands
StabilizationCommands
and a set of test classes, that use the command classes to perform scripted tests:
ConfigurationTests
StabilizationTests
These are then invoked via the GUI to run prescripted tests by our QA team. However, as the firmware is changed quite quickly between the releases it would be great if a developer could write an XML file that could invoke either the tests or the commands:
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite>
<StabilizationTests>
<StressTest repetition="10" />
</StabilizationTests>
<BasicConfigurationCommands>
<SelectConfig number="2" />
<ChangeConfigProperties name="Weeeeee" timeOut="15000" delay="1000"/>
<ApplyConfig />
</BasicConfigurationCommands>
</testsuite>
I have been looking at the System.Reflection
class and have seen examples using GetMethod
and then Invoke
. This requires me to create the class object at compile time and I would like to do all of this at runtime.
I would need to scan the whole assembly for the class name and then scan for the method within the class.
This seems a large solution, so any information pointing me (and future readers of this post) towards an answer would be great!
Thanks for reading,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查找程序集中的所有类:
实际上,您应该使用属性标记您的类,这使它们更容易被发现。
要实例化所述类的实例:
不要忘记,您可能应该用适当的 try/catch 块包装它们
然后在类上查找方法
只需使用 VStudio 中的对象浏览器并学习反射类,您可以找到很多可以做。
To Find all classes in an assembly:
In reality you should tag your class with an attribute, it makes them more discoverable.
To instantiate an instance of said class:
Don't forget, you should probably wrap these with appropriate try/catch blocks
Then to find a method on a class
Just play around with the Object Browser in VStudio and learn the Reflection classes, there's lots you can do.