Button_click()
{
str="MessageBox.show("无法找到参数!")"
method_test(str)
}method_test(string str)
{
//此处要执行Str所包含的内容....}
=========================
请问以上如何处理?
{
str="MessageBox.show("无法找到参数!")"
method_test(str)
}method_test(string str)
{
//此处要执行Str所包含的内容....}
=========================
请问以上如何处理?
method_test(ref string str)
{
//此处要执行Str所包含的内容....}
{
str="MessageBox.show("无法找到参数!")"
method_test(str)
}method_test(string str)
{
//此处要执行Str所包含的内容....
//现在Str=“MessageBox.show("无法找到参数!")",如何执行Str所包含的语名行呢?
}
{ System.Windows.Forms.MessageBox.Show
("Click!"); }; 它可以起类似的效果你的要求可以用Microsoft.CSharp.Compiler class实现
using System.CodeDom.Compiler;
using System.Reflection;private void method_test(string ACode)
{
ICodeCompiler vCodeCompiler = new CSharpCodeProvider().CreateCompiler();
CompilerParameters vCompilerParameters = new CompilerParameters();
vCompilerParameters.GenerateExecutable = false;
vCompilerParameters.GenerateInMemory = true;
vCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
string vSource =
"using System.Windows.Forms;\n" +
"public class Temp\n" +
"{\n" +
" public void Test()\n" +
" {\n" +
" " + ACode + "\n" +
" }\n" +
"}\n";
CompilerResults vCompilerResults =
vCodeCompiler.CompileAssemblyFromSource(vCompilerParameters, vSource);
Assembly vAssembly = vCompilerResults.CompiledAssembly;
object vTemp = vAssembly.CreateInstance("Temp");
MethodInfo vTest = vTemp.GetType().GetMethod("Test");
vTest.Invoke(vTemp, null);
}private void button1_Click(object sender, EventArgs e)
{
string str = "MessageBox.Show(\"无法找到参数!\");";
method_test(str);
}