0

我为 codeDOM 编译器创建了一个表单,如果它在单个文本文件中,它可以编译我的代码,但我希望能够编译文本文件中的源代码和文本文件中的类,以便它们可以一起工作.

我不确定如何对 codeDOM 进行编码以将我的类文件添加为要编译的主要源的资源

这是我到目前为止所拥有的

codeDOM 编译器

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;


namespace CodeDOMSourceTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnCompile_Click(object sender, EventArgs e)
        {
            CompilerParameters Params = new CompilerParameters();
            Params.GenerateExecutable = true;
            Params.ReferencedAssemblies.Add("System.dll");
            Params.ReferencedAssemblies.Add("TextFile1.txt");

            Params.OutputAssembly = "output.exe";

            string Source = Properties.Resources.CodeDOMSource;
            Source = Source.Replace("[TEXT]", txtReplace.Text);

            CompilerResults Results = new CSharpCodeProvider().CompileAssemblyFromSource(Params, Source);

            if (Results.Errors.Count > 0)
            {
                foreach (CompilerError err in Results.Errors)
                    Console.WriteLine(err.ToString());
            }
            else Console.WriteLine("Compiled just fine!");
        }
    }
}

源文件

namespace testingCodeDOM
{
class Program
{
static void Main()
{
System.Console.WriteLine("[TEXT]");
Console.WriteLine(Testing.textwork());
System.Console.Read();
}
}
}

类文件

namespace testingCodeDOM
{
    class Testing
    {
        public static string textwork()
        {
            string hello = "calss worked";
            return hello;
        }enter code here
    }
}

任何想法如何做到这一点,因为我已经用谷歌搜索了它并且我没有得到任何想法,或者至少我没有理解

另一方面,这不适用于源代码,但我正在尝试将其调整为使用类文件

4

1 回答 1

0

CompileAssemblyFromSource()方法可以采用任意数量的源代码字符串(因为它是一个params方法)。所以,你可以这样称呼它:

CompileAssemblyFromSource(Params, Source, ClassFile)

包含第二个文件文本的ClassFilea在哪里。string

于 2013-04-14T20:01:12.943 回答