Domanda

i have created a form for a codeDOM compiler and it can compile my code if its in a single text file but i want to be able to compile the source code in the text file and a class thats in a text file so they can work together.

I am unsure how to code codeDOM to add my class file as a resource for the main source to be compiled

Heres what i have so far

codeDOM Compiler

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!");
        }
    }
}

Source file

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

ClassFile

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

Any ideas how to do this because i have googled it an am getting no were or at least nothing i understand

on a side not this works with with just the source but am trying to adapt it to use class files aswell

È stato utile?

Soluzione

The CompileAssemblyFromSource() method can take any number of source code strings (since it's a params method). So, you can call it something like:

CompileAssemblyFromSource(Params, Source, ClassFile)

Where ClassFile is a string that contains the text of the second file.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top