vbnet動態編譯
① 鍦╒isual Studio涓鐢╒B.Net濡備綍鍒涘緩鑷宸辯殑鍔ㄦ佽繛鎺ュ簱dll
鏂板緩---綾誨簱---鎶婁綘鐨勫嚱鏁版斁鍦ㄦ柊寤虹殑綾誨簱涓-----鐢熸垚ClassLibrary1
鍦╪et紼嬪簭涓---寮曠敤----瀵瑰簲鐩褰曚笅鐨凜lassLibrary1.dll
瀵瑰簲鐨勪唬鐮佸畾涔夊傦細
Dim
mydll
As
New
ClassLibrary1.Class1
鏈鍚庡氨鍙浠ョ敤mydll璋冪敤浣犵殑鍑芥暟MyFunction浜嗭細
mydll
.MyFunction(1,
2)
娉ㄦ剰錛歷b.net緙栧啓鐨凞LL涓嶈兘鐢
API鍑芥暟
鐨勬柟寮忛氳繃Declare瀹e憡錛屽彧鑳藉紩鐢ㄧ殑鏂瑰紡浣跨敤
vb.net瑕佺紪鍐欑殑DLL鑳借玽b6.0寮曠敤錛岄渶瑕佸厛鍦ㄧ紪璇戝睘鎬т腑閫夋嫨鈥滀負COM
浜掓搷浣
娉ㄥ唽鈥濇墦鍕
鐒跺悗鍦╒isual
Studio
2008
鍛戒護鎻愮ず涓榪愮敤RegAsm鍛戒護鍔/tlb鍛戒護鐢熸垚tlb鏂囦歡
渚嬪傦細c:\Release>RegAsm
ClassLibrary1.dll
/tlb鍥炶濺灝變細鍦ㄥ綋鍓嶇洰褰曠敓鎴怌lassLibrary1.tlb鏂囦歡
vb6.0涓寮曠敤ClassLibrary1.tlb鍗沖彲銆
② 濡備綍鍦.NET涓瀹炵幇鑴氭湰寮曟搸
NET鏈韜鎻愪緵浜嗗己澶х殑鑴氭湰寮曟搸錛屽彲浠ョ洿鎺ヤ嬌鐢.NETCLR鐨勪換浣曠紪紼嬭璦浣滀負鑴氭湰璇璦錛屽俈B.NET銆丆#銆丣Script,J#絳夌瓑銆備嬌鐢ㄨ剼鏈寮曟搸錛屾垜浠鍙浠ュ姩鎬佺敓鎴愪換鎰忚〃杈懼紡銆佹垨鍔ㄦ佸煎叆浠繪剰鑴氭湰鏂囦歡錛屽苟鍦ㄤ換鎰忔椂鍊欐墽琛屻傜粡瀹炶返鍙戠幇錛屾垜浠鍙浠ヤ嬌鐢ㄨ嚦灝戜袱縐嶄笉鍚岀殑鏂瑰紡鍦.NET涓浣跨敤鑴氭湰寮曟搸錛歏saEngine鍜孋odeDom銆傚叾瀹烇紝CodeDom涓嶈兘綆楁槸鐪熸g殑鑴氭湰寮曟搸錛屽畠瀹為檯涓婃槸緙栬瘧鍣ㄣ備絾鏄鎴戜滑瀹屽叏鍙浠ュ埄鐢–odeDom鏉ユā鎷熻剼鏈寮曟搸銆備嬌鐢‥mit鏂規硶涔熻兘杈懼埌鍔ㄦ佺敓鎴愬彲鎵ц屼唬鐮佺殑鐩鐨勶紝鑰屼笖Emit鐢熸垚鐨勪唬鐮佷笉闇瑕佺紪璇戱紝鍥犳ら熷害鏇村揩銆備絾鏄疎mit鎻掑叆鐨勫疄闄呬笂鏄奼囩紪浠g爜錛屼笉鑳界畻鏄鑴氭湰璇璦銆傛湰鏂囦粙緇嶅備綍浠CodeDom鏂瑰紡鏉ュ姩鎬佺敓鎴愬彲鎵ц屼唬鐮併傚備綍鍦.NET涓瀹炵幇鑴氭湰寮曟搸(CodeDom綃)娌愭灚緗戝織1.鏋勯犱竴涓緙栬瘧鍣ㄨ劇疆緙栬瘧鍙傛暟緙栬瘧鍙傛暟闇瑕佸湪CompilerParameters璁劇疆錛欳ompilerOptions鐢ㄤ簬璁劇疆緙栬瘧鍣ㄥ懡浠よ屽弬鏁癐ncludeDebugInformation鐢ㄤ簬鎸囩ず鏄鍚﹀湪鍐呭瓨鍦ㄧ敓鎴怉ssemblyGenerateInMemory鐢ㄤ簬鎸囩ず鏄鍚﹀湪鍐呭瓨鍦ㄧ敓鎴怉ssemblyGenerateExecutable鐢ㄤ簬鎸囩ず鐢熸垚鐨凙ssembly綾誨瀷鏄痚xe榪樻槸dllOutputAssembly鐢ㄤ簬鎸囩ず鐢熸垚鐨勭▼搴忔枃浠跺悕錛堜粎鍦℅enerateInMemory涓篺alse鐨勬儏鍐碉級ReferencedAssemblies鐢ㄤ簬娣誨姞寮曠敤Assembly渚嬪傦細theParameters.ReferencedAssemblies.Add("System.dll");鍒涘緩鎸囧畾璇璦鐨勭紪璇戝櫒緙栬瘧闇瑕佺敱鎸囧畾璇璦鐨凜odeDomProvider鐢熸垚銆傝繖閲屽垪涓句竴浜.NET鐨凜odeDomProvider錛歷b.netMicrosoft.VisualBasic.VBCodeProviderC#Microsoft.CSharp..JScript.JScriptCodeProviderJ#Microsoft.VJSharp.VJSharpCodeProvider浠C#涓轟緥錛岃佸壋寤篊#緙栬瘧鍣錛屼唬鐮佸備笅錛//.NET1.1/1.0ICodeCompilercompiler=newMicrosoft.CSharp.CSharpCodeProvider().CreateCompiler();//.NET2.0ICodeCompilercompiler=(ICodeCompiler)newMicrosoft.CSharp.CSharpCodeProvider();涓嬮潰鏄瀹屾暣鐨勫壋寤虹紪璇戝櫒鐨勪緥瀛愶細//////鍒涘緩鐩稿簲鑴氭湰璇璦鐨勭紪璇戝櫒///privatevoidcreateCompiler(stringstrLanguage,booldebugMode,stringstrAssemblyFileName){this.theParameters=newCompilerParameters();this.theParameters.OutputAssembly=System.IO.Path.Combine(System.IO.Path.GetTempPath(),strAssemblyFileName+".dll");this.theParameters.GenerateExecutable=false;this.theParameters.GenerateInMemory=true;if(debugMode){this.theParameters.IncludeDebugInformation=true;this.theParameters.CompilerOptions+="/define:TRACE=1/define:DEBUG=1";}else{this.theParameters.IncludeDebugInformation=false;this.theParameters.CompilerOptions+="/define:TRACE=1";}AddReference("System.dll");AddReference("System.Data.dll");AddReference("System.Xml.dll");strLanguage=strLanguage.ToLower();CodeDomProvidertheProvider;if("visualbasic"==strLanguage||"vb"==strLanguage){theProvider=newMicrosoft.VisualBasic.VBCodeProvider();if(debugMode)theParameters.CompilerOptions+="/debug:full/optimize-/optionexplicit+/optionstrict+/optioncompare:text/imports:Microsoft.VisualBasic,System,System.Collections,System.Diagnostics";elsetheParameters.CompilerOptions+="/optimize/optionexplicit+/optionstrict+/optioncompare:text/imports:Microsoft.VisualBasic,System,System.Collections,System.Diagnostics";AddReference("Microsoft.VisualBasic.dll");}elseif("jscript"==strLanguage||"js"==strLanguage){theProvider=newMicrosoft.JScript.JScriptCodeProvider();AddReference("Microsoft.JScript.dll");}elseif("csharp"==strLanguage||"cs"==strLanguage||"c#"==strLanguage){theProvider=newMicrosoft.CSharp.CSharpCodeProvider();if(!debugMode)theParameters.CompilerOptions+="/optimize";}//elseif("jsharp"==strLanguage||"vj"==strLanguage||"j#"==strLanguage)//{//theProvider=newMicrosoft.VJSharp.VJSharpCodeProvider();//if(!debugMode)//theParameters.CompilerOptions+="/optimize";//}elsethrownewSystem.Exception("鎸囧畾鐨勮剼鏈璇璦涓嶈鏀鎸併");this.theCompiler=theProvider.CreateCompiler();}//////娣誨姞寮曠敤瀵硅薄銆//////寮曠敤鐨勬枃浠跺悕publicvoidAddReference(string__strAssemblyName){theParameters.ReferencedAssemblies.Add(__strAssemblyName);}娉錛氬湪.NETFramework2.0涓錛岀敱浜嶤reateCompiler鏂規硶琚鏍囪頒綔搴熴備負閬垮厤浜х敓緙栬瘧璀﹀憡錛屽彲鐩存帴榪斿洖CodeDomProvider浣滀負緙栬瘧鍣錛歵his.theCompiler=(ICodeCompiler)theProvider;2.緙栬瘧婧愪唬鐮佺紪璇戞簮浠g爜鐩稿綋綆鍗曪紝鍙闇涓鏉¤鍙ュ氨鎼炲畾浜嗭細錛漜ompiler.CompileAssemblyFromSource(this.theParameters,this.SourceText);鎵ц屽悗錛屽彲浠ヤ粠compilerResults鍙栧緱浠ヤ笅鍐呭癸細NativeCompilerReturnValue緙栬瘧緇撴灉錛岀敤浜庢鏌ユ槸鍚︽垚鍔烢rrors緙栬瘧鏃朵駭鐢熺殑閿欒鍜岃﹀憡淇℃伅CompiledAssembly濡傛灉緙栬瘧鎴愬姛錛屽垯榪斿洖緙栬瘧鐢熸垚鐨凙ssembly紺轟緥鍑芥暟錛//////緙栬瘧鑴氭湰銆傜紪璇戝墠灝嗘竻絀轟互鍓嶇殑緙栬瘧淇℃伅銆///CompilerInfo灝嗗寘鍚緙栬瘧鏃朵駭鐢熺殑閿欒淇℃伅銆//////鎴愬姛鏃惰繑鍥濼rue銆備笉鎴愬姛涓篎alse銆俻ublicboolCompile(){this.theCompilerInfo="";this.isCompiled=false;this.theCompiledAssembly=null;this.theCompilerResults=this.theCompiler.CompileAssemblyFromSource(this.theParameters,this.SourceText);if(this.theCompilerResults.NativeCompilerReturnValue==0){this.isCompiled=true;this.theCompiledAssembly=this.theCompilerResults.CompiledAssembly;}System.Text.StringBuildercompilerInfo=newSystem.Text.StringBuilder();foreach(CompilerErrorerrinthis.theCompilerResults.Errors){compilerInfo.Append(err.ToString());compilerInfo.Append("/r/n");}theCompilerInfo=compilerInfo.ToString();returnisCompiled;}3.鎵ц屼唬鐮佷嬌鐢≧eflection鏈哄埗灝卞彲浠ュ緢鏂逛究鐨勬墽琛孉ssembly涓鐨勪唬鐮併傛垜浠鍋囪劇紪璇戞椂浣跨敤鐨勮剼鏈浠g爜this.SourceText鍐呭瑰備笅錛歯amespacetest{publicclassscript{staticpublicvoidMain(){MessageBox.Show("Hello");}}}鍒欑浉搴旂殑鎵ц屼唬鐮佷負錛歴criptEngine.Invoke("test.script","Main",null);Invoke鍑芥暟鍐呭癸細//////鎵ц屾寚瀹氱殑鑴氭湰鍑芥暟(Method)銆///濡傛灉鎸囧畾鐨勭被鎴栨ā鍧楀悕錛屼互鍙婂嚱鏁(Method)銆佹垨鍙傛暟涓嶆g『錛屽皢浼氫駭鐢烿saException/VshException渚嬪栥//////綾繪垨妯″潡鍚///瑕佹墽琛岀殑鍑芥暟(Method)鍚嶅瓧///鍙傛暟(鏁扮粍)///榪斿洖鎵ц岀殑緇撴灉publicobjectInvoke(string__strMole,string__strMethod,object[]__Arguments){if(!this.IsCompiled||this.theCompiledAssembly==null)thrownewSystem.Exception("鑴氭湰榪樻病鏈夋垚鍔熺紪璇");Type__MoleType=this.theCompiledAssembly.GetType(__strMole);if(null==__MoleType)thrownewSystem.Exception(string.Format("鎸囧畾鐨勭被鎴栨ā鍧({0})鏈瀹氫箟銆",__strMole));MethodInfo__MethodInfo=__MoleType.GetMethod(__strMethod);if(null==__MethodInfo)thrownewSystem.Exception(string.Format("鎸囧畾鐨勬柟娉({0}::{1})鏈瀹氫箟銆",__strMole,__strMethod));try{return__MethodInfo.Invoke(null,__Arguments);}catch(TargetParameterCountException){thrownewSystem.Exception(string.Format("鎸囧畾鐨勬柟娉({0}:{1})鍙傛暟閿欒銆",__strMole,__strMethod));}catch(System.Exceptione){System.Diagnostics.Trace.WriteLine(string.Format("鎵ц({0}:{1})閿欒:{2}",__strMole,__strMethod,e.ToString()));returnnull;}}鎬葷粨錛欳odeDom鍙浠ュ緢鏂逛究鐨勯殢鏃剁紪璇戞簮浠g爜錛屽苟鍔ㄦ佹墽琛屻傝櫧鐒朵綔涓鴻剼鏈寮曟搸錛屽畠娌℃湁VsaEngine姝h勫拰鏂逛究錛屼絾浣滀負涓鑸搴旂敤錛屼篃澶熺敤浜嗐傚苟涓旂粨鍚圧eflection鏈哄埗錛屽畠鐨勫姛鑳芥瘮VsaEngine鏇村己澶э細瀹冨彲浠ョ紪璇戜換浣曟彁渚汣ompilerProvider鐨凜LR璇璦錛堢洰鍓.NET鑷甯︾殑璇璦涓閮芥湁錛夈傚綋鐒訛紝瀹冧篃鏈変竴浜涚己鐐癸細瀹冪敓鎴愮殑Assembly涓嶈兘鍔ㄦ佸嵏杞姐傝繖鍦ㄤ竴鑸鎯呭喌涓嬩笉鎴愰棶棰橈紝鍥犱負涓涓婧愪唬鐮佸彧闇緙栬瘧涓嬈★紝騫惰澆鍏ユ墽琛岋紝騫朵笉闇瑕佸姩鎬佸嵏杞姐傚亣濡備綘闇瑕佸仛鑴氭湰緙栬緫鍣ㄦ椂錛屽氨瑕佽冭檻榪欎釜闂棰橈紝鍥犱負鏈夊彲鑳戒竴涓鑴氭湰浼氬洜涓轟慨淇鏀規敼鑰屼笉鍋滅殑閲嶆柊緙栬瘧錛屼粠鑰岄犳垚涓嶅仠鐨勪駭鐢熸柊鐨凙ssembly錛屾渶鍚庡皢瀵艱嚧鍐呭瓨琚澶ч噺鍗犵敤銆傝佽В鍐寵繖涓闂棰橈紝闇瑕佸皢緙栬瘧鍣ㄥ姞杞藉埌鐙絝嬬殑AppDomain涓錛岄氳繃鍗歌澆AppDomain杈懼埌鍗歌澆鎵闇鐨凙ssembly鐨勭洰鐨勩
③ VB.NET 2008 動態編譯,在RichTextBox1 中填入代碼,點擊Button1運行該代碼,在Textbox中顯示結果
添加引用MicrosoftScriptControl
ImportsMSScriptControl
PublicClassForm1
.ScriptControl
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
ScripObj.Language="VBScript"
EndSub
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
Try
ScripObj.AddCode(RichTextBox1.Text)
ScripObj.Run("a",TextBox1)
CatchexAsException
MsgBox(ex.Message)
EndTry
EndSub
EndClass
'在文本框中輸入的函數
suba(obj)
dima,b
a=40:b=30
ifa>bthen
obj.text="A>B"
else
obj.text="A<B"
endif
endsub