解讀.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實現程序的插件機制。