看了網(wǎng)上很多關(guān)于DotNet動(dòng)態(tài)創(chuàng)建類的實(shí)例的文章,我這里想總結(jié)一下,其實(shí)方法很簡(jiǎn)單,就是用“Activator.CreateInstance”。但是這個(gè)方法需要待創(chuàng)建的類的Type作為參數(shù),為了獲得該參數(shù),可以利用[Assembly].GetType方法,這個(gè)方法只需要待創(chuàng)建的類的名稱(名稱字符串)就可以了,最后的問題就是要獲得這個(gè)類所在的程序集。如何獲得待創(chuàng)建的類所在程序集,那么就解決了這個(gè)問題。
大家可以參考http://www.cnblogs.com/ShadowK/archive/2006/11/14/560131.html,費(fèi)了很多筆墨寫了一個(gè)比較完整的動(dòng)態(tài)構(gòu)造類的設(shè)計(jì)器。其實(shí),在獲得程序集這個(gè)問題上,可以有更簡(jiǎn)單的辦法,以下是我的做法。
利用Microsoft.VisualBasic.VBCodeProvider(),如果是C#可以用CSharpCodeProvider(),將類文件編譯成為DLL文件,然后利用[Assembly].LoadFrom("DLL 的絕對(duì)路徑")加載該DLL。這樣我們可以避免在那些創(chuàng)建DLL和Type的復(fù)雜代碼。我告訴我的項(xiàng)目組成員這個(gè)例子后,強(qiáng)調(diào)要打開思路,Simple is perfect,凡事都盡量找簡(jiǎn)便的方法來實(shí)現(xiàn),客戶永遠(yuǎn)不會(huì)為我們那些復(fù)雜的代碼多花一分錢。
1.執(zhí)行編譯任務(wù)的方法:
Public Shared Function CompileExecutable()Function CompileExecutable(ByVal sourceName As String, ByVal DLLPath As String, ByRef ReturnDLLName As String) As Boolean
Dim sourceFile As FileInfo = New FileInfo(sourceName)
Dim provider As CodeDomProvider = Nothing
Dim compileOk As Boolean = False
' 根據(jù)原文件的擴(kuò)展名選擇code provider
If sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".CS" Then
provider = New Microsoft.CSharp.CSharpCodeProvider()
ElseIf sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) = ".VB" Then
provider = New Microsoft.VisualBasic.VBCodeProvider()
Else
Console.WriteLine("原文件必須包含 .cs 或 .vb 擴(kuò)展名")
End If
If Not provider Is Nothing Then
' 構(gòu)造DLL文件的全路徑
Dim dllName As String = String.Format("{0}\{1}.dll", _
DLLPath, _
sourceFile.Name.Replace(".", "_"))
ReturnDLLName = dllName
Dim cp As CompilerParameters = New CompilerParameters()
' 設(shè)置編譯控制參數(shù)
cp.GenerateExecutable = False '生成DLL,如果是True則生成exe文件
cp.OutputAssembly = dllName
cp.GenerateInMemory = False
cp.TreatWarningsAsErrors = False
' 調(diào)用編譯方法將原代碼文件編譯成DLL
Dim cr As CompilerResults = provider.CompileAssemblyFromFile(cp, _
sourceName)
If cr.Errors.Count > 0 Then
' 顯示編譯錯(cuò)誤
Console.WriteLine("編譯錯(cuò)誤 {0} 編譯成 {1}", _
sourceName, cr.PathToAssembly)
Dim ce As CompilerError
For Each ce In cr.Errors
Console.WriteLine(" {0}", ce.ToString())
Console.WriteLine()
Next ce
Else
' 顯示編譯成功的消息
Console.WriteLine("原文件 {0} 編譯成 {1} 成功完成.", _
sourceName, cr.PathToAssembly)
End If
' 返回編譯結(jié)果
If cr.Errors.Count > 0 Then
compileOk = False
Else
compileOk = True
End If
End If
Return compileOk
End Function
2.編譯DLL,并動(dòng)態(tài)創(chuàng)建類的實(shí)例。(這里類的原文件是Class1.vb文件,放在WebSite的App_Code文件夾中了,實(shí)際使用時(shí)可以放在任意物理位置。)
Dim strSourceFileName As String = Server.MapPath("~/App_Code/Class1.vb") '類文件的全路徑
Dim strDllPath As String = Server.MapPath("~/App_Code") '編譯后的DLL文件存放的位置
Dim strDllName As String = "" 'DLL的全路徑(返回值)
CompileExecutable(strSourceFileName, strDllPath, strDllName) '編譯原文件為DLL文件
Dim a As [Assembly] = [Assembly].LoadFrom(strDllName) '加載DLL
Dim myType As System.Type = a.GetType("Class1") '獲得Class1的Type
Dim obj As Object = Activator.CreateInstance(myType) '獲得Class1的實(shí)例
3.Class1.vb原文件
Public Class Class1Class Class1
Public i As Integer
End Class