自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳細(xì)分析VB.NET動態(tài)代碼

開發(fā) 后端
這里介紹,.NET為我們提供了一系列VB.NET動態(tài)代碼選項(xiàng)。例如,你可以創(chuàng)建一個可執(zhí)行的能獨(dú)立運(yùn)行的程序或是可以想運(yùn)行中的程序加載一個DLL然后再執(zhí)行。

學(xué)習(xí)VB.NET時,經(jīng)常會遇到使用VB.NET動態(tài)代碼問題,這里將介紹使用VB.NET動態(tài)代碼問題的解決方法,在這里拿出來和大家分享一下。

使用VB.NET動態(tài)代碼

在運(yùn)行時創(chuàng)建一個控件是在無法確定應(yīng)用程序功能的時候采取的一種策略。但是動態(tài)創(chuàng)建控件并不適用于所有的情況。有些時候你必須建立可執(zhí)行代碼,雖然你的應(yīng)用程序運(yùn)行的目的是補(bǔ)償不同極其之間的配置,不同用戶的需求,不同的環(huán)境需求或是其他要求。當(dāng)應(yīng)用程序所運(yùn)行的電腦不存在控件,那么通常是需要創(chuàng)建VB.NET動態(tài)代碼的。

幸運(yùn)的是,.NET為我們提供了一系列VB.NET動態(tài)代碼選項(xiàng)。例如,你可以創(chuàng)建一個可執(zhí)行的能獨(dú)立運(yùn)行的程序或是可以想運(yùn)行中的程序加載一個DLL然后再執(zhí)行。當(dāng)你需要演示一個外部任務(wù)的時候可以使用選擇可執(zhí)行,如運(yùn)行一種腳本——該DLL選項(xiàng)最適合擴(kuò)大現(xiàn)有的應(yīng)用程序功能。

你可以運(yùn)行來自文件或內(nèi)存的VB.NET動態(tài)代碼。當(dāng)你需要不止一次地運(yùn)行代碼時,可以使用文件。對代碼的檢查可以再次運(yùn)行外部文件而不需要對其進(jìn)行二次編譯。當(dāng)你需要多次演示任務(wù)的時候,如一個安裝請求,那可以使用內(nèi)存圖像。

當(dāng)然我們也可以更改源代碼。例如,你可以使用字符串來建立需要在應(yīng)用程序中直接使用的代碼。如果你需要代碼具有高度靈活性,且代碼本身不是很長時,這一方法的優(yōu)勢就非常顯著。也可以從文件里建立代碼,就如同VS一樣。這一方法最適用于相對穩(wěn)定且不需要復(fù)雜編碼的需求。第三種選擇是使用 Documentation Object Model來創(chuàng)建代碼并將其作為CodeDom樹型結(jié)構(gòu)的一個系列。該樹型結(jié)構(gòu)包括了CodeCormpileUnits.這就像是用DOM模式創(chuàng)建了一個XML文件。

使用動態(tài)創(chuàng)建代碼的***方式是用示例來檢查一下。例三展示了一個基本“Hello World”示例。該示例用源代碼直接創(chuàng)建了代碼因此你可以看到整個運(yùn)行以及生成一個外部可執(zhí)行文件的過程。

  1. Private Sub btnTest3_Click() Handles btnTest3.Click     
  2. ' Create a compiler.    
  3. Dim Comp As VBCodeProvider = New VBCodeProvider()    
  4. ' Define the parameters for the code you want to compile.    
  5. Dim Parms As CompilerParameters = New CompilerParameters)    
  6.    
  7. ' We do want to create an executable, rather than a DLL.    
  8. Parms.GenerateExecutable = True   
  9. ' The compiler will create an output assembly called Output.    
  10. Parms.OutputAssembly = "Output"   
  11. ' The compiler won't treat warnings as errors.    
  12. Parms.TreatWarningsAsErrors = False   
  13. ' Add any assembly you want to reference.    
  14. Parms.ReferencedAssemblies.Add("System.Windows.Forms.dll")    
  15.    
  16. ' Define the code you want to run.    
  17.    
  18. Dim SampleCode As StringBuilder = New StringBuilder()    
  19.    
  20. SampleCode.Append("Imports System.Windows.Forms" + vbCrLf)    
  21.    
  22. SampleCode.Append("Module TestAssembly" + vbCrLf)    
  23.    
  24. SampleCode.Append("Sub Main()" + vbCrLf)    
  25.    
  26. SampleCode.Append("MessageBox.Show(" + Chr(34) + _    
  27. "Dynamically Created Code!" + _Chr(34) + ")" + vbCrLf)    
  28. SampleCode.Append("End Sub" + vbCrLf)    
  29. SampleCode.Append("End Module" + vbCrLf)    
  30. ' Define the code to run.    
  31. Dim Executable As CompilerResults = _   
  32. Comp.CompileAssemblyFromSource(Parms, SampleCode.ToString())    
  33. ' Display error messages if there are any.    
  34. If Executable.Errors.HasErrors Then    
  35. For Each Item As CompilerError In Executable.Errors    
  36. MessageBox.Show(Item.ErrorText)    
  37. Next    
  38. Else    
  39. ' If there aren't any error messages, start the    
  40. ' executable.    
  41. Process.Start("Output")    
  42. End If    
  43. End Sub  

【編輯推薦】

  1. 淺談VB.NET線程構(gòu)造器
  2. 簡單分析VB.NET使用線程
  3. VB.NET List(T)編寫框架方法
  4. 簡單介紹VB.NET線程同步
  5. VB.NET聲明API詳細(xì)描述
責(zé)任編輯:佚名 來源: IT168
相關(guān)推薦

2009-10-28 10:06:29

VB.NET With

2009-10-10 15:19:43

VB.NET Web

2010-01-07 17:00:38

VB.NET控件數(shù)組

2009-10-14 10:25:52

VB.NET讀寫文本文

2009-10-10 13:52:57

VB Update方法

2010-01-06 13:50:37

.NET Framew

2009-10-12 15:02:51

VB.NET動態(tài)控件

2009-09-25 14:23:39

2009-09-28 10:39:01

Hibernate基礎(chǔ)

2009-11-16 11:18:38

PHP上傳圖片代碼

2009-11-11 14:18:00

動態(tài)路由協(xié)議

2010-03-04 09:30:40

Linux動態(tài)庫

2009-09-14 13:50:35

LINQ編程模型

2009-09-08 15:56:50

Linq使用Group

2009-11-20 13:11:44

Oracle XML數(shù)

2009-06-18 14:00:51

2009-09-09 09:48:43

Linq延遲加載

2009-09-14 16:21:34

LINQ To XML

2009-10-27 09:59:17

VB.NET動態(tài)代碼

2009-10-28 09:55:29

VB.NET MyCl
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號