0

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

leppie
  • 115,091
  • 17
  • 196
  • 297
Chris Devine
  • 3,859
  • 2
  • 13
  • 7
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Apr 14 '13 at 19:36

1 Answers1

0

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.

svick
  • 236,525
  • 50
  • 385
  • 514
  • thanks it worked! :D is it not possible to put 'using system;' at the top of the text file instead in 'Params' because i intend to use it on my other program and that uses a lot of references – Chris Devine Apr 14 '13 at 20:16