列表B
privateITest CreateTest(string assemblyPath, string typeName)
{
//Create an ObjectHandle object that will hold an instance of the requested
// type in the given assembly. The ObjectHandle class is in the
// System.Remoting namespace.
ObjectHandle handle = Activator.CreateInstanceFrom(assemblyPath, typeName);
//Unwrap the object instance into an object class.
object test = handle.Unwrap();
//Test to make sure the object implements the ITest interface. Any object
// that does not implement the ITest interface will cause an exception to
// be thrown.
if (test isITest)
return (ITest)test; //Return the object as an ITest instance.
else
thrownewException("This Type does not implement ITest, which is required.
Assembly: " + assemblyPath + "
Type: " + typeName);
}
插件层
插件层中包含为应用程序建立的所有插件。一般来说,这个层中包含的插件执行共享对象层中的一个或几个接口。这允许主应用程序层与插件进行通信。
插件层由TestExamples项目执行。每个测试的功能各不相同,对于测试的功能也没有特别规定。唯一的规定就是测试必须执行ITest接口,其它功能由测试开发者决定。
列表C是一个非常简单的名为“MathTest”的测试实例。如果1不等于2(1!=2),则测试通过;如果1等于2(1==2),则测试失败:
列表C
using System;
using System.Collections.Generic;
using System.Text;
using SystemTests.SharedObjects;
namespace SystemTests.TestExamples
{
publicclassMathTest : Test, ITest
{
publicoverridevoid Execute(TestEngineState state)
{
//Set the ID for this test.
this.ID = "MathTest";
//Set the Name of this test.
this.Name = "Math Test";
//Set the description of this test.
this.Description = "Tests to see if 1 = 2";
//Instantiate a TestResult object to hold the
// results of this test.
TestResult result = newTestResult();
//Very simple test to see if 1 = 2
if (1 == 2)
result.Passed = true;
else
result.Passed = false;
//Set the result.
this.Result = result;
}
}
}
虽然这个测试十分简单,如上所述,只要它执行ITest接口,它就能实现任何功能。
本文中的解决方案包含另外两个样本测试:
- SuperManTest – 决定Superman类是否执行IsuperHero接口。
- WebTest – 决定本地计算机是否能够访问http://microsoft.com/,(如果您使用代理,这个测试可能失败。)
本文包含的解决方案中还有另外一个名为“TestApplication”的项目。这个项目是一个非常简单的Windows应用程序,它显示TestEngine的结果。
应用插件解决自己的问题
了解本文中包含的C#和.NET Framework解决方案,考虑如何将这种类型的插件功能用于解决您所在公司的商业问题。可能至少有一些情形适用这种高度动态化的功能。在适当的时候使用本文中的代码实例!
责任编辑:张琎
查看本文国际来源 |