接口
接口是插件行为的描述。 接口不包括任何业务逻辑实现,业务逻辑包含在实现接口的类中。
接口声明:
public interface IPlus
{
///<summary>
///插件行为方法
///</summary>
void DoSomething();
}
类的实现:
public class Plus1: IPlus
{
#region IPlus成员
void IPlus.DoSomething()
{
MessageBox.Show(GetType().ToString() + " Do somthing...");
}
#endregion
}
插件化系统中提倡通过接口的方式来访问对象,也可以使用虚基类来实现整个系统。使用接口和使用类,在业界也是经常引起争议的话题,笔者比较推荐用接口来实现。
反射
反射主要理解为在未能引用类的类型定义情况下创建和访问对象的能力。
名字空间Kaitu.System中包含了Form类。 一般情况下当需要创建一个Form类的对象时, 必须引用Kaitu.System命名空间, 然后声明Form类型的对象,然后用new操作创建对象。
Form frm = new Form(); //一般类对象的创建。
假如Form类的定义暂时不能引用到当前的开发环境下 , 但时程序在运行期却是能创建并访问它, 这个时候就要利用反射机制来创建该对象:
比如:
using System.Reflection;
//加载运行期的程序集。
Assembly SampleAssembly = Assembly.LoadFile(@"c:\kaitulib.dll");
//创建程序集中包含的类对象。
object instance = SampleAssembly.CreateInstance("Guost.Lib.PlusA");
首先是通过程序集的文件名加载程序集对象, 这里类似设计期对程序集的引用。然后是通过类的名称字符串创建类对象,从这里可以看出,创建一个类对象不再用new的方式,而是通过程序集名称和类名字符串创建的。 这里可能有人要问,既然不引用程序集,那么怎样访问创建后的对象呢?用接口,反射创建的类对象都实现了接口,而接口我们在应用程序设计期是可见的。
IPlus myPlusObject = instance as IPlus; //将对象转到接口
MyPlusObject.DoSomething(); //调用业务逻辑
1
using System.Data;
2
using System.IO;
3
using System.Drawing;
4
using MyInterface;
5
using System.Text;
6
using System.Windows.Forms;
7
using System.Reflection;
8
9
namespace pathTest
10

{
11
public partial class Form1 : Form
12
{
13
public Form1()
14
{
15
InitializeComponent();
16
}
17
private Dictionary<String, inter> plusList = new Dictionary<string, inter>();
18
private void Form1_Load(object sender, EventArgs e)
19
{
20
找路径#region 找路径
21
String path = Application.StartupPath + @"\plusTest";
22
textBox1.Text = path;
23
#endregion
24
创建目录#region 创建目录
25
DirectoryInfo dirInfo = new DirectoryInfo(path);
26
#endregion
27
遍历目录所有文件提取目的dll中的类型#region 遍历目录所有文件提取目的dll中的类型
28
FileInfo[] fileInfos = dirInfo.GetFiles();
29
foreach (FileInfo fi in fileInfos)
30
{
31
if (fi.Extension.Equals(".dll") && fi.FullName.IndexOf("testLib") != -1)
32
{
33
Assembly SampleAssembly;
34
try
35
{
36
SampleAssembly = Assembly.LoadFile(fi.FullName);
37
Type[] objType = SampleAssembly.GetTypes();
38
反射机制创建对象#region 反射机制创建对象
39
//反射机制创建对象
40
for (int i = 0; i < objType.Length; i++)
41
{
42
this.listBox1.Items.Add(objType[i].ToString());
43
44
object plus = SampleAssembly.CreateInstance(objType[i].ToString());
45
//把对象转嫁为接口
46
把对象转嫁为接口#region 把对象转嫁为接口
47
inter myinter = plus as inter;
48
plusList.Add(objType[i].ToString(), myinter);
49
# endregion
50
51
}
52
#endregion
53
}
54
catch (Exception ex)
55
{
56
MessageBox.Show(ex.Message,"提示信息");
57
}
58
59
}
60
}
61
#endregion
62
63
}
64
65
private void listBox1_Click(object sender, EventArgs e)
66
{
67
string key = listBox1.Items[this.listBox1.SelectedIndex].ToString();
68
inter myinter = plusList[key];
69
textBox2.Text = myinter.Show();
70
}
71
}
72
}
以下是两个dll. 一个是MyInterface.dll, 一个是TestLib.dll.其中MyInterface.dll是主程序框架中写好后编译成dll后暴露给插件编程者的,插件编程者引用该dll.另一个TestLib.dll.以插件的形式给出,不需要程序里引用,通过程序集对象的CreatInstance( classname )方法来创建对象实例,创建好的对象实例转到接口然后调用接口中的行为。
bject plus = SampleAssembly.CreateInstance(objType[i].ToString());
inter myinter = plus as inter;
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
6
namespace MyInterface
7

{
8
public interface inter
9
{
10
String Show();
11
}
12
}
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using MyInterface;
5
6
namespace testLib
7

{
8
public class Family:inter
9
{
10
String motherName = "miaomiao";
11
String fatherName = "fox";
12
String sisterName = "big cat";
13
String myselfName = "hehe";
14
15
public String MotherName
16
{
17
get
{ return motherName; }
18
set
{ motherName = value; }
19
}
20
public String FatherName
21
{
22
get
{ return fatherName; }
23
set
{ fatherName = value; }
24
}
25
public String SisterName
26
{
27
get
{ return sisterName; }
28
set
{ sisterName = value; }
29
}
30
public String MyselfName
31
{
32
get
{ return myselfName; }
33
set
{ myselfName = value; }
34
}
35
36
public Family()
37
{ }
38
String inter.Show()
39
{
40
String res = "Mother:" + motherName + ",Father:" + fatherName + ",Sister:" + sisterName + ",And Me " + myselfName;
41
return res;
42
}
43
}
44
45
public class BFriend:inter
46
{
47
String bfName = "tutu";
48
String myName = "miaomiao";
49
public String BfName
50
{
51
get
{ return bfName; }
52
set
{ bfName = value; }
53
}
54
public String MyName
55
{
56
get
{ return myName; }
57
set
{ myName = value; }
58
}
59
public BFriend()
60
{
61
62
}
63
String inter.Show()
64
{
65
String res = bfName + " like " + myName + ",and " + myName + " like " + bfName;
66
return res;
67
}
68
}
69
}
70
posted on 2008-10-29 17:53
天书 阅读(2693)
评论(1) 编辑 收藏 引用