【发布时间】:2011-06-10 18:30:26
【问题描述】:
我想从http://pastehtml.com/view/awono3xoq.txt 下载代码,将其保存为字符串,然后在单击按钮时编译并运行它,但我似乎无法让下面的代码工作:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
namespace ASV
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void compile()
{
CSharpCodeProvider myCodeProvider = new CSharpCodeProvider();
ICodeCompiler myCodeCompiler = myCodeProvider.CreateCompiler();
String [] referenceAssemblies = {"System.dll"};
string myAssemblyName = "Assembly.exe";
CompilerParameters myCompilerParameters = new CompilerParameters(referenceAssemblies, myAssemblyName);
myCompilerParameters.GenerateExecutable = true;
myCompilerParameters.GenerateInMemory = true;
WebClient x = new WebClient();
Stream y = x.OpenRead("http://pastehtml.com/view/awono3xoq.txt");
StreamReader z = new StreamReader(y);
string source = z.ReadToEnd();
z.Close();
y.Close();
CompilerResults compres = myCodeCompiler.CompileAssemblyFromSource(myCompilerParameters, source);
Process.Start("Assembly.exe");
}
private void button1_Click(object sender, EventArgs e)
{
compile();
}
}
}
我做错了什么(除了有太多的使用状态:P)?
【问题讨论】:
-
你需要说 what 不起作用。你有错误吗?如果是这样,完全的例外是什么?如果它没有按照您的预期去做,请说明它的行为与您的预期有何不同。
-
它可以编译吗?你有任何错误吗?是否抛出异常?
-
在循环计数器之外使用单字母变量名是下班后跳进停车场的好方法。
-
啊。对不起,我得到的错误来自运行“Process.Start(“Assembly.exe”);错误是系统找不到指定的文件。当 GenerateInMemory = false 时也会发生这种情况。我认为出于某种原因它没有t 编译它,但它仍然不会抛出错误
标签: c# runtime compilation