【问题标题】:Using CodeDOM to source code and class file togther使用 CodeDOM 源代码和类文件一起
【发布时间】:2013-04-14 19:34:16
【问题描述】:

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

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

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

【问题讨论】:

标签: c# codedom system-codedom-compiler


【解决方案1】:

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

CompileAssemblyFromSource(Params, Source, ClassFile)

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

【讨论】:

  • 感谢它的工作! :D 是不是不可能把'使用系统;'在文本文件的顶部而不是在“参数”中,因为我打算在我的其他程序上使用它并且使用了很多引用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多