开发者社区


C++/C | Java | .Net/Windows | Linux/Unix | 开源 | 其他
首页 - 编程
编程
全文搜索:   

如何用C#和.NET Framework开发自己的插件构架
作者: Builder.com.cn
2006-12-03 10:19:28



列表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解决方案,考虑如何将这种类型的插件功能用于解决您所在公司的商业问题。可能至少有一些情形适用这种高度动态化的功能。在适当的时候使用本文中的代码实例!

责任编辑:张琎

查看本文国际来源

【上一页】
声明:
Builder.com.cn(原ZDNet China应用开发频道)原创文章版权所有,未经许可严禁转载,且不构成投资建议。
近期相关报道:
实用技术文档
J2me XML
C/C++ C#
Java Oracle
Mysql .Net
VB.NET CSS
SQL Server 数据库
SQL UNIX
Linux Jsp
PHP Perl
Javascript IIS
XHTML ColdFusion
ASP/ASP.NET Apache
AJAX
订阅技术邮件
订阅"技术圈"杂志!请在下面选择您感兴趣的专题,填写e-mail地址,然后按订阅按钮:
应用开发管理
VS.NET 周刊
Database 周刊
WEB Service周刊
JAVA 周刊
IT 认证
Windows服务器周刊
互联网开发
当Windows Server 2008专家得5000元现金大奖
CNET NETWORKS 中国: 爱卡汽车网 | CNET科技资讯网 | e询网 | CWEEK | 蜂鸟网 | GameSpot China | 个人电脑 | PChome | SPN | 网友世界 | ZDNet China | 中关村在线
CNET NETWORKS 美国: BNET | CNET.com | CNET Download.com | CNET News.com | CNET Reviews | CNET Shopper.com | GameSpot | MP3.com | mySimon | Release 1.0 | Search.com | TechRepublic | TV.com | Webshots | ZDNet
Copyright (c) 2006 CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
ZDNet 公司标识是 CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号