解读.Net实现程序的插件机制_.Net教程
教程Tag:暂无Tag,欢迎添加,赚取U币!
推荐:解读软件系统与软件系统之间的集成交互问题可能写得有些乱,希望能谅解,同时我整理的思想,希望能给您带来一些启发或者帮助。 我们假设一个国际化的大公司里,有来自多个国家的员工,这些员工又工作在不同的部门里,负责不同的任务。 首先: 1. 这些人可能是白人,也可能是黑人,也可能是黄皮肤的我
使用asp.net或vb.net或C#.net均可以实现插件的功能,插件的作用是对系统的后期未知扩展预留快速升级的空间!插件开发完成后将以DLL的形式发布到特定的文件夹中,例如主程序文件夹是D:\myApp\myApp.exe,我们可以在D:\myApp中定义文件夹D:\myApp\Plus目录来存放插件。
在程序中通过遍历该文件夹中的所有DLL检测符合预定接口的插件来实现动态装载相应的功能模块!
下面举例:
一、定义接口
新建解决方案用于生成此接口
1 Public Class IPlugin
2 Public Interface IPlugin
3 Property MovementID() As Integer
4 Property MovementName() As String
5 Property ReTryMinute() As Integer
6 Property ConnectionString() As String
7 Event RaiseLog(ByVal log As String)
8 Sub Init()
9 End Interface
10 End Class
11
2 Public Interface IPlugin
3 Property MovementID() As Integer
4 Property MovementName() As String
5 Property ReTryMinute() As Integer
6 Property ConnectionString() As String
7 Event RaiseLog(ByVal log As String)
8 Sub Init()
9 End Interface
10 End Class
11
二、编写继承自该接口的自定义组件(将编译成DLL)
该操作您需要新建一个解决方案
1Imports System.Data
2Imports System.Data.SqlClient
3Imports System.Net
4Imports System.IO
5Imports na7Interface.IPlugin
6
7Public Class recvClass recv
8
9 Implements IPlugin '实现此接口
10
11 '构造属性变量
12 Private _MovementID As Integer = 27
13 Private _MovementName As String = "51麦克疯"
14 Private _SpanMinute As Integer = 30
15 Private _ConnectionString As String = ""
16
17 Private Fir As String = "活动:" + MovementName + "(" + MovementID.ToString + ")"
18
19 /**/''' <summary>
20 ''' 返回内部处理的消息,实现接口中的RaiseLog事件。
21 ''' </summary>
22 ''' <param name="logText"></param>
23 ''' <remarks></remarks>
24 Public Event RaiseMyEvent(ByVal logText As String) Implements IPlugin.RaiseLog
25
26 /**/''' <summary>
27 ''' 调用此方法!
28 ''' </summary>
29 ''' <remarks></remarks>
30 Public Sub Init()Sub Init() Implements IPlugin.Init '继承自接口的Init方法。
31 '这里放置数据抓取及相关的处理程序。
32 '实际应用中要根据您的程序何时调用Init决定,这不是必须的方法
33 ResultText("处理完成!")
34 End Sub
35
36 Private Sub SaveSetups()Sub SaveSetups(ByVal savedate As String, ByVal UserID As String, ByVal WebId As String, ByVal Setups As String)
37 '定义该内部方法,这个方法不实现任何接口中的方法。因此只在本类内部实现。
38 Dim CmdText As String = ""
39 End Sub
40
41 /**/''' <summary>
42 ''' 返回八位数字的日期格式
43 ''' </summary>
44 ''' <param name="dt"></param>
45 ''' <returns></returns>
46 ''' <remarks></remarks>
47 Private Function ClongDate()Function ClongDate(ByVal dt As Date) As String '同上
48 Dim mm As String = dt.Month.ToString
49 If mm.Length = 1 Then mm = "0" + mm
50 Dim dd As String = dt.Day.ToString
51 If dd.Length = 1 Then dd = "0" + dd
52 Return dt.Year.ToString + "" + mm + dd
53 End Function
54
55 Private Function ConvDate()Function ConvDate(ByVal dt As Long) As String '同上
56 Dim YY As String = Mid(dt.ToString, 1, 4)
57 Dim MM As String = Mid(dt.ToString, 5, 2)
58 Dim DD As String = Mid(dt.ToString, 7, 2)
59 Return YY + "-" + MM + "-" + DD + " 00:00:00"
60 End Function
61
62 /**/''' <summary>
63 ''' 获得此组件处理的活动ID,该属性实现了接口IPlugin的MovementID属性。
64 ''' </summary>
65 ''' <value></value>
66 ''' <returns></returns>
67 ''' <remarks></remarks>
68 Public Property MovementID()Property MovementID() As Integer Implements IPlugin.MovementID
69 Get
70 Return _MovementID
71 End Get
72 Set(ByVal value As Integer)
73 _MovementID = value
74 End Set
75 End Property
76
77 /**/''' <summary>
78 ''' 获得此组件的活动名称,同上
79 ''' </summary>
80 ''' <value></value>
81 ''' <returns></returns>
82 ''' <remarks></remarks>
83 Public Property MovementName()Property MovementName() As String Implements IPlugin.MovementName
84 Get
85 Return _MovementName
86 End Get
87 Set(ByVal value As String)
88 _MovementName = value
89 End Set
90 End Property
91
92 /**/''' <summary>
93 ''' 获得此组件定时调用的间隔分钟数,实现自定义属生ReTryMinute
94 ''' </summary>
95 ''' <value></value>
96 ''' <returns></returns>
97 ''' <remarks></remarks>
98 Public Property SpanMinute()Property SpanMinute() As Integer Implements IPlugin.ReTryMinute
99 Get
100 Return _SpanMinute
101 End Get
102 Set(ByVal value As Integer)
103 _SpanMinute = value
104 End Set
105 End Property
106
107 Public Property ConnectionString()Property ConnectionString() As String Implements IPlugin.ConnectionString
108 Get
109 Return _ConnectionString
110 End Get
111 Set(ByVal value As String)
112 _ConnectionString = value
113 End Set
114 End Property
115
116 Public Sub ResultText()Sub ResultText(ByVal txt As String)
117 '响应事件
118 RaiseEvent RaiseMyEvent(txt)
119 End Sub
120End Class
121
2Imports System.Data.SqlClient
3Imports System.Net
4Imports System.IO
5Imports na7Interface.IPlugin
6
7Public Class recvClass recv
8
9 Implements IPlugin '实现此接口
10
11 '构造属性变量
12 Private _MovementID As Integer = 27
13 Private _MovementName As String = "51麦克疯"
14 Private _SpanMinute As Integer = 30
15 Private _ConnectionString As String = ""
16
17 Private Fir As String = "活动:" + MovementName + "(" + MovementID.ToString + ")"
18
19 /**/''' <summary>
20 ''' 返回内部处理的消息,实现接口中的RaiseLog事件。
21 ''' </summary>
22 ''' <param name="logText"></param>
23 ''' <remarks></remarks>
24 Public Event RaiseMyEvent(ByVal logText As String) Implements IPlugin.RaiseLog
25
26 /**/''' <summary>
27 ''' 调用此方法!
28 ''' </summary>
29 ''' <remarks></remarks>
30 Public Sub Init()Sub Init() Implements IPlugin.Init '继承自接口的Init方法。
31 '这里放置数据抓取及相关的处理程序。
32 '实际应用中要根据您的程序何时调用Init决定,这不是必须的方法
33 ResultText("处理完成!")
34 End Sub
35
36 Private Sub SaveSetups()Sub SaveSetups(ByVal savedate As String, ByVal UserID As String, ByVal WebId As String, ByVal Setups As String)
37 '定义该内部方法,这个方法不实现任何接口中的方法。因此只在本类内部实现。
38 Dim CmdText As String = ""
39 End Sub
40
41 /**/''' <summary>
42 ''' 返回八位数字的日期格式
43 ''' </summary>
44 ''' <param name="dt"></param>
45 ''' <returns></returns>
46 ''' <remarks></remarks>
47 Private Function ClongDate()Function ClongDate(ByVal dt As Date) As String '同上
48 Dim mm As String = dt.Month.ToString
49 If mm.Length = 1 Then mm = "0" + mm
50 Dim dd As String = dt.Day.ToString
51 If dd.Length = 1 Then dd = "0" + dd
52 Return dt.Year.ToString + "" + mm + dd
53 End Function
54
55 Private Function ConvDate()Function ConvDate(ByVal dt As Long) As String '同上
56 Dim YY As String = Mid(dt.ToString, 1, 4)
57 Dim MM As String = Mid(dt.ToString, 5, 2)
58 Dim DD As String = Mid(dt.ToString, 7, 2)
59 Return YY + "-" + MM + "-" + DD + " 00:00:00"
60 End Function
61
62 /**/''' <summary>
63 ''' 获得此组件处理的活动ID,该属性实现了接口IPlugin的MovementID属性。
64 ''' </summary>
65 ''' <value></value>
66 ''' <returns></returns>
67 ''' <remarks></remarks>
68 Public Property MovementID()Property MovementID() As Integer Implements IPlugin.MovementID
69 Get
70 Return _MovementID
71 End Get
72 Set(ByVal value As Integer)
73 _MovementID = value
74 End Set
75 End Property
76
77 /**/''' <summary>
78 ''' 获得此组件的活动名称,同上
79 ''' </summary>
80 ''' <value></value>
81 ''' <returns></returns>
82 ''' <remarks></remarks>
83 Public Property MovementName()Property MovementName() As String Implements IPlugin.MovementName
84 Get
85 Return _MovementName
86 End Get
87 Set(ByVal value As String)
88 _MovementName = value
89 End Set
90 End Property
91
92 /**/''' <summary>
93 ''' 获得此组件定时调用的间隔分钟数,实现自定义属生ReTryMinute
94 ''' </summary>
95 ''' <value></value>
96 ''' <returns></returns>
97 ''' <remarks></remarks>
98 Public Property SpanMinute()Property SpanMinute() As Integer Implements IPlugin.ReTryMinute
99 Get
100 Return _SpanMinute
101 End Get
102 Set(ByVal value As Integer)
103 _SpanMinute = value
104 End Set
105 End Property
106
107 Public Property ConnectionString()Property ConnectionString() As String Implements IPlugin.ConnectionString
108 Get
109 Return _ConnectionString
110 End Get
111 Set(ByVal value As String)
112 _ConnectionString = value
113 End Set
114 End Property
115
116 Public Sub ResultText()Sub ResultText(ByVal txt As String)
117 '响应事件
118 RaiseEvent RaiseMyEvent(txt)
119 End Sub
120End Class
121
三、在程序中调用定义的接口
注意:程序中定义接口编译生成的DLL必须使用同一个,否则如果不是使用相同的DLL,即使DLL的内容相同,同样也会产生错误。
分享:如何创建安全的Web应用程序把一个问题说清楚,是要有前提的,也要大家有一些通过的词汇,就像大家谈OO,就知道谈的面向对象,以及面向对象的一些特征;懂设计模式的,一听到
相关.Net教程:
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP发送Email实例(可带附件)
- js实现广告漂浮效果的小例子
- asp.net Repeater 数据绑定的具体实现
- Asp.Net 无刷新文件上传并显示进度条的实现方法及思路
- Asp.net获取客户端IP常见代码存在的伪造IP问题探讨
- VS2010 水晶报表的使用方法
- ASP.NET中操作SQL数据库(连接字符串的配置及获取)
- asp.net页面传值测试实例代码
- DataGridView - DataGridViewCheckBoxCell的使用介绍
- asp.net中javascript的引用(直接引入和间接引入)
- 三层+存储过程实现分页示例代码
- 相关链接:
- 教程说明:
.Net教程-解读.Net实现程序的插件机制。