使用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
二、編寫繼承自該接口的自定義組件(將編譯成DLL)
該操作您需要新建一個解決方案
以下為引用的內容:
1Imports System.Data
2Imports System.Data.SqlClient
3Imports System.Net
4Imports System.IO
5Imports na7Interface.IPlugin
6
7Public Class 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() Implements IPlugin.Init '繼承自接口的Init方法。
31 '這裡放置數據抓取及相關的處理程序。
32 '實際應用中要根據您的程序何時調用Init決定,這不是必須的方法
33 ResultText("處理完成!")
34 End Sub
35
36 Private 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(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(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() 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() 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() 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() 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(ByVal txt As String)
117 '響應事件
118 RaiseEvent RaiseMyEvent(txt)
119 End Sub
120 End Class
三、在程序中調用定義的接口
注意:程序中定義接口編譯生成的DLL必須使用同一個,否則如果不是使用相同的DLL,即使DLL的內容相同,同樣也會產生錯誤。
以下為引用的內容:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Reflection
Imports PlusFace.Face
Public Class ShowPlus
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
loadPlus()
End Sub
Private Sub loadPlus()
'遍歷Plus目錄下的dll文件
dllFileName.Items.Add(New ListItem("請選擇接口插件", ""))
Dim DirPath As String = Server.MapPath("..\Interface")
If IO.Directory.Exists(DirPath) = False Then
IO.Directory.CreateDirectory(DirPath)
End If
Dim DirectoryPath() As String
DirectoryPath = System.IO.Directory.GetFileSystemEntries(DirPath)
For Each file As String In DirectoryPath
If System.IO.File.Exists(file) = True Then
'判斷此文件擴展名是否為dll
If LCase(Strings.Right(file, 4)) = ".dll" Then
'獲得此文件信息及此組件的入口類名稱與
Dim Ref As Assembly
Ref = Assembly.LoadFrom(file)
For Each typeTmp As Type In Ref.GetTypes
If typeTmp.IsPublic = True Then
'下面判斷是否實現了接口,如果是,則取當前類型為該類實現的類型
Dim TypeGet As Type = typeTmp.GetInterface("Plus")
If IsNothing(TypeGet) = False Then
Dim asmName As String = System.IO.Path.GetFileNameWithoutExtension(file)
Dim t As Type = Ref.GetType(typeTmp.FullName) '這是區分大小寫的。
Dim Obj As Plus = CType(Activator.CreateInstance(t), Plus)
'下面加載插件
Dim FileName As String = Replace(file, Server.MapPath("..\Interface\"), "")
Response.Write(Obj.InterfaceName + "(" + FileName + ")")
Exit For
End If
End If
Next
End If
Else
End If
Next
End Sub
End Class
這裡有本人2007年底花四個月時間寫成的進銷存系統,裡面使用了插件機制生成的報表設計管理器,但沒有使用到接口,而是預先定義好調用的方法。