当前位置:首页 » 编程软件 » vbnet动态编译

vbnet动态编译

发布时间: 2024-07-26 08:22:32

① 鍦╒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
热点内容
newman算法 发布:2024-11-25 21:34:55 浏览:200
a算法概念 发布:2024-11-25 21:24:16 浏览:587
jquery源码书籍 发布:2024-11-25 21:19:50 浏览:803
银行卡输入密码超限怎么办 发布:2024-11-25 21:09:07 浏览:958
编译指令多发 发布:2024-11-25 20:58:17 浏览:751
java上传文件到服务器 发布:2024-11-25 20:52:47 浏览:741
轴加工编程 发布:2024-11-25 20:52:12 浏览:412
手机的媒体存储 发布:2024-11-25 20:29:42 浏览:265
安卓如何关闭手机桌面 发布:2024-11-25 20:24:37 浏览:701
脚本也违法吗 发布:2024-11-25 20:24:24 浏览:305