解析 XML 函数名称并在整个程序集中调用

发布于 2024-09-01 14:02:20 字数 1203 浏览 1 评论 0原文

我编写了一个通过互联网浏览器对我们的硬件进行单元测试的应用程序。

我在程序集中有命令类,它们是各个 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 类,并看到使用 GetMethodInvoke 的示例。这要求我在编译时创建类对象,并且我想在运行时完成所有这些

我需要扫描整个程序集以查找类名,然后扫描类中的方法。

这似乎是一个很大的解决方案,因此任何指向我(以及这​​篇文章的未来读者)找到答案的信息都会很棒!

感谢您的阅读,

马特

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-09-08 14:02:20

查找程序集中的所有类:

public Type FindClass(string name)
{
   Assembly ass = null;
   ass = Assembly.Load("System.My.Assembly"); // Load from the GAC
   ass = Assembly.LoadFile(@"c:\System.My.Assembly.dll"); // Load from file
   ass = Assembly.LoadFrom("System.My.Assembly"); // Load from GAC or File

   foreach(Type t in ass.GetTypes())
   {
      if (t.Name == name)
         return t;
   }

   return null;
}

实际上,您应该使用属性标记您的类,这使它们更容易被发现。

要实例化所述类的实例:

public T Instantiate<T>(Type typ, object[] arguments)
{
    return (T)Activator.CreateInstance(typ, arguments, null);
}

不要忘记,您可能应该用适当的 try/catch 块包装它们

然后在类上查找方法

Type t = FindClass("MyType");
MethodInfo meth = t.GetMethod("TestSomething", typeof(string), typeof(int)); // finds public ??? TestSomething(string, int)

只需使用 VStudio 中的对象浏览器并学习反射类,您可以找到很多可以做。

To Find all classes in an assembly:

public Type FindClass(string name)
{
   Assembly ass = null;
   ass = Assembly.Load("System.My.Assembly"); // Load from the GAC
   ass = Assembly.LoadFile(@"c:\System.My.Assembly.dll"); // Load from file
   ass = Assembly.LoadFrom("System.My.Assembly"); // Load from GAC or File

   foreach(Type t in ass.GetTypes())
   {
      if (t.Name == name)
         return t;
   }

   return null;
}

In reality you should tag your class with an attribute, it makes them more discoverable.

To instantiate an instance of said class:

public T Instantiate<T>(Type typ, object[] arguments)
{
    return (T)Activator.CreateInstance(typ, arguments, null);
}

Don't forget, you should probably wrap these with appropriate try/catch blocks

Then to find a method on a class

Type t = FindClass("MyType");
MethodInfo meth = t.GetMethod("TestSomething", typeof(string), typeof(int)); // finds public ??? TestSomething(string, int)

Just play around with the Object Browser in VStudio and learn the Reflection classes, there's lots you can do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文