說我有這樣的事情:如何執行字符串中的代碼?
string singleStatement = "System.DateTime.Now";
是否有某種方式採取singleStatement
和分析,並在運行時運行呢?
這樣:
DateTime currentTime = singleStatement.SomeCoolMethodToRunTheText();
會的DateTime.Now
值分配給currentTime
。
說我有這樣的事情:如何執行字符串中的代碼?
string singleStatement = "System.DateTime.Now";
是否有某種方式採取singleStatement
和分析,並在運行時運行呢?
這樣:
DateTime currentTime = singleStatement.SomeCoolMethodToRunTheText();
會的DateTime.Now
值分配給currentTime
。
只有使用編譯器服務和Reflection.Emit()
纔可以編譯和彙編並加載到內存中。
看看這裏。
您可以使用編譯使用CSharpCodeProvider對象在運行時代碼。無論你是否真的想要這樣做,都需要辯論。 :-)
閱讀this(引用如下)。
這是可能的:看看System.CodeDom
和System.CodeDom.Compiler
。
我發現我幾個月前寫了一個例子: 假設usingList
是arraylist
所有using語句(不使用 關鍵字,System.Xml
例如) 假設importList
是arraylist
與所必需的 所有DLL名稱編譯(system.dll
例如) 假設source
是要編譯源代碼 假設classname
是要編譯 承擔methodname
類的名稱是方法的名稱
看一看下面的代碼:
//Create method
CodeMemberMethod pMethod = new CodeMemberMethod();
pMethod.Name = methodname;
pMethod.Attributes = MemberAttributes.Public;
pMethod.Parameters.Add(new
CodeParameterDeclarationExpression(typeof(string[]),"boxes"));
pMethod.ReturnType=new CodeTypeReference(typeof(bool));
pMethod.Statements.Add(new CodeSnippetExpression(@"
bool result = true;
try
{
" + source + @"
}
catch
{
result = false;
}
return result;
"));
//Crée la classe
CodeTypeDeclaration pClass =
new System.CodeDom.CodeTypeDeclaration(classname);
pClass.Attributes = MemberAttributes.Public;
pClass.Members.Add(pMethod);
//Crée le namespace
CodeNamespace pNamespace = new CodeNamespace("myNameSpace");
pNamespace.Types.Add(pClass);
foreach(string sUsing in usingList)
pNamespace.Imports.Add(new
CodeNamespaceImport(sUsing));
//Create compile unit
CodeCompileUnit pUnit = new CodeCompileUnit();
pUnit.Namespaces.Add(pNamespace);
//Make compilation parameters
CompilerParameters pParams =
new CompilerParameters((string[])importList.ToArray(typeof(string)));
pParams.GenerateInMemory = true;
//Compile
CompilerResults pResults =
(new CSharpCodeProvider())
.CreateCompiler().CompileAssemblyFromDom(pParams, pUnit);
if (pResults.Errors != null && pResults.Errors.Count>0)
{
foreach(CompilerError pError in pResults.Errors)
MessageBox.Show(pError.ToString());
result =
pResults.CompiledAssembly.CreateInstance("myNameSp ace."+classname);
}
例如,
if 'usingList' equals
{
"System.Text.RegularExpressions"
}
if 'importList' equals
{
"System.dll"
}
if 'classname' equals "myClass"
if 'methodName' equals "myMethod"
if 'source' equals "
string [email protected]"ES
FR
EN
"
Regex regex=new Regex(@"^[A-Za-z]
{
2
}
$");
result=regex.IsMatch(boxes[0]);
if (result)
{
regex=new Regex(@"^"+boxes[0][email protected]".$",RegexOptions.Multiline);
result=regex.Matches(pays).Count!=0;
}
然後將編譯的代碼如下所示:
using System.Text.RegularExpressions;
namespace myNameSpace
{
public class myClass
{
public bool myMethod(string[] boxes)
{
bool result=true;
try
{
string [email protected]"ES
FR
EN
"
Regex regex=new Regex(@"^[A-Za-z]
{
2
}
$");
result=regex.IsMatch(boxes[0]);
if (result)
{
regex=new Regex(@"^"+boxes[0][email protected]".$",RegexOptions.Multiline);
result=regex.Matches(pays).Count!=0;
}
}
catch
{
result=false;
}
return result;
}
}
}
我可以查詢你爲什麼想要? – dotalchemy 2011-01-26 00:11:44
http://scriptcs.net/? :) – NicoJuicy 2014-08-22 08:15:26