private void button1_Click(object sender, System.EventArgs e)
{
this.richTextBox1.Text=" int a=3\n int b=3;\n int c=a+b;\n this.richTextBox2.Text=c.toString();";
}private void button2_Click(object sender, System.EventArgs e)
{
 //这里要显示richTextBox1里所生成的代码的值,就是在richTextBox2显示C的值
}望能给出实现代码,机器上没装MSDN~~

解决方案 »

  1.   

    脚本中有这样的方法,比如js中的eval(),VBS中的Execute()
      

  2.   

    可是 做的是WINFROM啊  不是网页啊……  怎么用脚本
      

  3.   

    九十年代时用foxpro2.5时,的"宏替换"(&..)方法可以这么做。但现在的语言好像没有明确支持这种方式编程。eval等似乎是唯一的办法。我也希望有人能说一下现在怎么这么干。
      

  4.   

    楼主是需要一个即时编译的功能是吗?看看非MS的即时编译器,可以编译WINFORM文本框中的代码,核心还是调FCL的:
    http://teach.spaceedu.com/A200508/2005-08-07/186389.html
    http://www.yomi.cn/computer/A200508/2005-08-07/186389.html另外可以发消息与飞刀联系探讨下:
    http://message.csdn.net/SendMessage.aspx?To=aspcn
      

  5.   

    我翻译的一篇文章,或许对你有帮助http://srw962.cnblogs.com/archive/2005/10/08/250557.html
      

  6.   

    这篇文章可能就是你感兴趣的,
    C# Script (missing puzzle piece)
    http://www.codeproject.com/csharp/cs-script_for_CP.asp
      

  7.   

    /*****************************************************************
    ** 文件名: Eval.cs
    ** Copyright (c) 1999 -2003 
    ** 创建人: Phoenix
    ** 创建日期: 
    ** 修改人:
    ** 修改日期: 
    ** 描 述: 获取字符串所表示的逻辑意义
    ** 版 本:1.0
    ******************************************************************/using System.CodeDom;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;
    using System.Reflection;public class Eval
    {
    static object GetValue( string value )
    {
    string codeSnippet = "using System; " + "\r\n" +
    "namespace CzG {" + "\r\n" +
    " public class Eval" + "\r\n" +
    " {" + "\r\n" +
    " public Eval(){} " + "\r\n" +
    " public object GetValue()" + "\r\n" +
    " {" + "\r\n" +
    " return " + value + ";" + "\r\n" +
    " }" + "\r\n" +
    " } }";CodeSnippetCompileUnit unit = new CodeSnippetCompileUnit( codeSnippet ); ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
    CompilerParameters para = new CompilerParameters();
    para.ReferencedAssemblies.Add( "System.dll" );
    para.GenerateInMemory = true;
    para.GenerateExecutable = false;
    para.OutputAssembly = "Eval.dll";Assembly asm = compiler.CompileAssemblyFromDom( para , unit ).CompiledAssembly;Type type = asm.GetType( "CzG.Eval" );
    MethodInfo mi = type.GetMethod( "GetValue" , BindingFlags.Public | BindingFlags.Instance ); object obj = asm.CreateInstance( "CzG.Eval" );return mi.Invoke( obj , null );
    }
    }一个例子
      

  8.   

    用MsScriptControl可以实现,你自己上网上可以搜到相关资料
      

  9.   

    可以看下CodeDom相关应用的资料,
    通过CodeDom动态生成程序集,通过反射来调用程序集里的东西。
    CodeDom的功能是很强大的。楼主还是装个MSDN吧。
      

  10.   

    public class Eval
    {
        static object GetValue(string value)
        {
            string codeSnippet = "using System; " + "\r\n" +
                                            "namespace CzG {" + "\r\n" +
                                            " public class Eval" + "\r\n" +
                                            " {" + "\r\n" +
                                            "       public Eval(){} " + "\r\n" +
                                            "  public object GetValue()" + "\r\n" +
                                            "  {" + "\r\n" +
                                            "   return " + value + ";" + "\r\n" +
                                            "  }" + "\r\n" +
                                            " } }";        CodeSnippetCompileUnit unit = new CodeSnippetCompileUnit(codeSnippet);        ICodeCompiler compiler = new CSharpCodeProvider().CreateCompiler();
            CompilerParameters para = new CompilerParameters();
            para.ReferencedAssemblies.Add("System.dll");
            para.GenerateInMemory = true;
            para.GenerateExecutable = false;
            para.OutputAssembly = "Eval.dll";        Assembly asm = compiler.CompileAssemblyFromDom(para, unit).CompiledAssembly;        Type type = asm.GetType("CzG.Eval");
            MethodInfo mi = type.GetMethod("GetValue", BindingFlags.Public | BindingFlags.Instance);        object obj = asm.CreateInstance("CzG.Eval");        return mi.Invoke(obj, null);
        }
        static void Main()
        {
            Console.WriteLine(Eval.GetValue("125 -23"));
            Console.WriteLine(Eval.GetValue("125<23"));
        }
    }
    仅供参考 相信这是你要想要的
      

  11.   

    怎么这么复杂楼主说的动态代码,是指动态的公式,然后动态计算吧?假设a,b是变量,strTemp是公式strTemp.Replace("a",a);
    strTemp.Replace("b",b);strResult = Evaluator.EvalToString(strTemp);strResult应该就是计算的结果。
      

  12.   

    http://community.csdn.net/Expert/topic/4485/4485930.xml?temp=.2340357