欧美日韩精品在线,国内精品久久久久久久久,一级毛片恃级毛片直播,清纯唯美亚洲综合欧美色

用.net動態(tài)創(chuàng)建類的實例_.Net教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:ASP.NET 2.0 里輸出文本格式流
在用 ASP.NET 編程時,打開一個頁面一般是通過指定超鏈接地址,調(diào)用指定的頁面文件(.html、.aspx)等方法。 但是,如果即將打開的頁面文件的內(nèi)容是在程序中動態(tài)生成,或者是從數(shù)據(jù)庫的表里取出

用.net動態(tài)創(chuàng)建類的實例

看了網(wǎng)上很多關(guān)于DotNet動態(tài)創(chuàng)建類的實例的文章,我這里想總結(jié)一下,其實方法很簡單,就是用“Activator.CreateInstance”。但是這個方法需要待創(chuàng)建的類的Type作為參數(shù),為了獲得該參數(shù),可以利用[Assembly].GetType方法,這個方法只需要待創(chuàng)建的類的名稱(名稱字符串)就可以了,最后的問題就是要獲得這個類所在的程序集。如何獲得待創(chuàng)建的類所在程序集,那么就解決了這個問題。

其實,在獲得程序集這個問題上,可以有更簡單的辦法,以下是我的做法。

利用Microsoft.VisualBasic.VBCodeProvider(),如果是C#可以用CSharpCodeProvider(),將類文件編譯成為DLL文件,然后利用[Assembly].LoadFrom("DLL 的絕對路徑")加載該DLL。這樣我們可以避免在那些創(chuàng)建DLL和Type的復(fù)雜代碼。我告訴我的項目組成員這個例子后,強調(diào)要打開思路,Simple is perfect,凡事都盡量找簡便的方法來實現(xiàn),客戶永遠不會為我們那些復(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ù)原文件的擴展名選擇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 擴展名")
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
' 顯示編譯錯誤
Console.WriteLine("編譯錯誤 {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,并動態(tài)創(chuàng)建類的實例。(這里類的原文件是Class1.vb文件,放在WebSite的App_Code文件夾中了,實際使用時可以放在任意物理位置。)

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的實例
3.Class1.vb原文件
Public Class Class1Class Class1
Public i As Integer
End Class

分享:從XML文件中讀取數(shù)據(jù)綁定到DropDownList
1 、綁定DropDownList: 以下為引用的內(nèi)容: ddl_language.DataSource = createDataSource(); ddl_language.DataTextField = "languageText

來源:模板無憂//所屬分類:.Net教程/更新時間:2008-08-22
相關(guān).Net教程