代碼的動態編譯並執行是一個
動態代碼執行可以應用在諸如模板生成
最基本的動態編譯
· 將要被編譯和執行的代碼讀入並以字符串方式保存
· 聲明CSharpCodeProvider對象實例
· 調用CSharpCodeProvider實例的CompileAssemblyFromSource方法編譯
· 用反射生成被生成對象的實例(Assembly
· 調用其方法
以下代碼片段包含了完整的編譯和執行過程
//get the code to compile
string strSourceCode = this
//
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
//
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters
objCompilerParameters
objCompilerParameters
//
CompilerResults cr = objCSharpCodePrivoder
if (cr
{
string strErrorMsg = cr
for (int x =
{
strErrorMsg = strErrorMsg +
cr
cr
}
this
MessageBox
return;
}
//
Assembly objAssembly = cr
object objClass = objAssembly
if (objClass == null)
{
this
return;
}
object[] objCodeParms = new object[
objCodeParms[
string strResult = (string)objClass
this
需要解釋的是
namespace Dynamicly
{
public class HelloWorld
{
public string GetTime(string strName)
{
return
}
}
}
運行附件中提供的程序
改進的執行過程
現在一切看起來很好
要解決這個問題我們需要來了解一下應用程序域
· 創建另外一個Application Domain
· 動態創建(編譯)代碼並保存到磁盤
· 創建一個公共的遠程調用接口
· 創建遠程調用接口的實例
換句話來講就是將對象加載到另外一個AppDomain中並通過遠程調用的方法來調用
using System;
using System
using System
using System
using System
namespace RemoteAccess
{
/// <summary>
/// Interface that can be run over the remote AppDomain boundary
/// </summary>
public interface IRemoteInterface
{
object Invoke(string lcMethod
}
/// <summary>
/// Factory class to create objects exposing IRemoteInterface
/// </summary>
public class RemoteLoaderFactory : MarshalByRefObject
{
private const BindingFlags bfi = BindingFlags
public RemoteLoaderFactory() {}
public IRemoteInterface Create( string assemblyFile
{
return (IRemoteInterface) Activator
assemblyFile
null
}
}
}
接下來在原來基礎上需要修改的是
· 將編譯成的DLL保存到磁盤中
· 創建另外的AppDomain
· 獲得IRemoteInterface接口的引用
· 調用InvokeMethod方法來遠程調用
· 可以通過AppDomain
以下是完整的代碼
//get the code to compile
string strSourceCode = this
//
AppDomainSetup objSetup = new AppDomainSetup();
objSetup
AppDomain objAppDomain = AppDomain
//
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
//
CompilerParameters objCompilerParameters = new CompilerParameters();
objCompilerParameters
objCompilerParameters
// Load the remote loader interface
objCompilerParameters
// Load the resulting assembly into memory
objCompilerParameters
objCompilerParameters
//
CompilerResults cr = objCSharpCodePrivoder
if (cr
{
string strErrorMsg = cr
for (int x =
{
strErrorMsg = strErrorMsg +
cr
cr
}
this
MessageBox
return;
}
//
RemoteLoaderFactory factory = (RemoteLoaderFactory)objAppDomain
// with help of factory
object objObject = factory
if (objObject == null)
{
this
return;
}
// *** Cast object to remote interface
IRemoteInterface objRemote = (IRemoteInterface)objObject;
object[] objCodeParms = new object[
objCodeParms[
string strResult = (string)objRemote
this
//Dispose the objects and unload the generated DLLs
objRemote = null;
AppDomain
System
對於客戶端的輸入程序
using System;
using System
using RemoteAccess;
namespace Dynamicly
{
public class HelloWorld : MarshalByRefObject
{
public object Invoke(string strMethod
{
return this
}
public string GetTime(string strName)
{
return
}
}
}
這樣
From:http://tw.wingwit.com/Article/program/net/201311/11574.html