繁体   English   中英

使用CodeDOM获取源代码和类文件

[英]Using CodeDOM to source code and class file togther

我已经为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
    }
}

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

一方面,这不适用于源代码,但也试图使其适应于使用类文件

CompileAssemblyFromSource()方法可以采用任意数量的源代码字符串(因为它是params方法)。 因此,您可以将其命名为:

CompileAssemblyFromSource(Params, Source, ClassFile)

其中, ClassFile是包含第二个文件的文本的string

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM