C# 在整个程序集中解析XML函数名和调用
我编写了一个应用程序,通过internet浏览器对硬件进行单元测试 我在程序集中有命令类,它们是单个web浏览器操作的包装,例如勾选复选框,从下拉框中选择:C# 在整个程序集中解析XML函数名和调用,c#,xml,system.reflection,C#,Xml,System.reflection,我编写了一个应用程序,通过internet浏览器对硬件进行单元测试 我在程序集中有命令类,它们是单个web浏览器操作的包装,例如勾选复选框,从下拉框中选择: BasicConfigurationCommands EventConfigurationCommands StabilizationCommands 以及一组测试类,它们使用命令类执行脚本测试: ConfigurationTests StabilizationTests 然后通过GUI调用它们,以运行QA团队规定的测试。但是,由于固件
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
的示例。这要求我在编译时创建类对象,我希望在运行时执行所有这些操作
我需要扫描整个程序集的类名,然后扫描类中的方法
这似乎是一个很大的解决方案,所以任何能让我(以及这篇文章的未来读者)找到答案的信息都是非常棒的
谢谢你的阅读
要查找程序集中的所有类,请执行以下操作:
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);
}
只需在VStudio中使用对象浏览器并学习反射类,您可以做很多事情
Type t = FindClass("MyType");
MethodInfo meth = t.GetMethod("TestSomething", typeof(string), typeof(int)); // finds public ??? TestSomething(string, int)