-2

我想运行包含表单的 jscript.net 代码,一些带有事件的按钮。所有这些都必须在 c# 中通过反射完成,但我找不到这些事件的体面示例。请给我一些帮助。这是我到目前为止所拥有的,但最后一行会有错误

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("import System.Windows.Forms;");
            sb.AppendLine("class EventTestForm extends Form");
            sb.AppendLine("{");
            sb.AppendLine(" var btn : Button;");
            sb.AppendLine("function EventTestForm()");
            sb.AppendLine("{");
            sb.AppendLine(" btn = new Button; btn.Text = \"Fire Event\";  Controls.Add(btn); btn.add_Click(ButtonEventHandler1);");
            sb.AppendLine("}");
            sb.AppendLine("function ButtonEventHandler1(sender, e : EventArgs)");
            sb.AppendLine("{");
            sb.AppendLine(" MessageBox.Show(\"Event is Fired!\");");
            sb.AppendLine("}");
            sb.AppendLine("}");

            sb.AppendLine("MessageBox.Show(\"Hello world\");");

            // Create the compiler object
            JScriptCodeProvider provider = new JScriptCodeProvider();
            ICodeCompiler compiler = provider.CreateCompiler();

            CompilerParameters options = new CompilerParameters();
            options.GenerateInMemory = true;

            options.GenerateExecutable = true;
            options.ReferencedAssemblies.Add("System.Windows.Forms");
            CompilerResults results = compiler.CompileAssemblyFromSource(options, sb.ToString());
4

3 回答 3

1

如果您尝试在 c# 应用程序中运行 javascript,最好使用其中一个 js 解释器

我用jint,还有侏罗纪,javascript.net,也许还有其他

于 2012-07-26T16:16:36.550 回答
0
options.GenerateExecutable = false;
options.GenerateInMemory = true;
options.ReferencedAssemblies.Add("system.dll");
options.ReferencedAssemblies.Add("system.Text.dll");
options.ReferencedAssemblies.Add("system.windows.forms.dll");

使用这些行

于 2014-04-23T15:12:35.840 回答
0

我可以建议你只学习 JavaScript 吗?刚刚看了一下JScriptCodeProvider课程,文档说:

此 API 支持 .NET Framework 基础结构,不打算直接从您的代码中使用。

http://msdn.microsoft.com/en-us/library/microsoft.jscript.jscriptcodeprovider.aspx

于 2012-07-27T08:01:59.253 回答