国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

用.net動態創建類的實例

2024-07-10 13:12:27
字體:
來源:轉載
供稿:網友

用.net動態創建類的實例

看了網上很多關于dotnet動態創建類的實例的文章,我這里想總結一下,其實方法很簡單,就是用“activator.createinstance”。但是這個方法需要待創建的類的type作為參數,為了獲得該參數,可以利用[assembly].gettype方法,這個方法只需要待創建的類的名稱(名稱字符串)就可以了,最后的問題就是要獲得這個類所在的程序集。如何獲得待創建的類所在程序集,那么就解決了這個問題。

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

利用microsoft.visualbasic.vbcodeprovider(),如果是c#可以用csharpcodeprovider(),將類文件編譯成為dll文件,然后利用[assembly].loadfrom("dll 的絕對路徑")加載該dll。這樣我們可以避免在那些創建dll和type的復雜代碼。我告訴我的項目組成員這個例子后,強調要打開思路,simple is perfect,凡事都盡量找簡便的方法來實現,客戶永遠不會為我們那些復雜的代碼多花一分錢。
 
1.執行編譯任務的方法:

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

' 根據原文件的擴展名選擇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

' 構造dll文件的全路徑
dim dllname as string = string.format("{0}/{1}.dll", _
dllpath, _
sourcefile.name.replace(".", "_"))

returndllname = dllname

dim cp as compilerparameters = new compilerparameters()

' 設置編譯控制參數
cp.generateexecutable = false '生成dll,如果是true則生成exe文件
cp.outputassembly = dllname
cp.generateinmemory = false
cp.treatwarningsaserrors = false

' 調用編譯方法將原代碼文件編譯成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

' 返回編譯結果
if cr.errors.count > 0 then
compileok = false
else
compileok = true
end if
end if
return compileok

end function


2.編譯dll,并動態創建類的實例。(這里類的原文件是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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 社会| 东源县| 西青区| 嘉荫县| 长乐市| 土默特左旗| 衡南县| 肇源县| 北京市| 蒙山县| 东乡县| 西吉县| 城固县| 清徐县| 北安市| 清水县| 福海县| 西安市| 布拖县| 承德市| 怀集县| 车险| 定结县| 乌拉特中旗| 安国市| 丹巴县| 祥云县| 手机| 彭山县| 班玛县| 盈江县| 平原县| 淮安市| 海林市| 建水县| 三穗县| 盐边县| 西藏| 顺平县| 吉木萨尔县| 蓝田县|