prizm1
prizm1

Reputation: 373

Compiling with CodeDom

I started experimenting a bit with CodeDom and made simple Application which collects sourcecode from the user input and tries to compile it with C#-Syntax.

For those who want to try the whole proccess, type end... to finish up the sourcecode entry.

Here's the example:

using System;
using System.Collections;
using System.Reflection;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.CSharp;
using System.CodeDom.Compiler;

namespace CodeDomTest
{
    class Program
    {
        static void Main(string[] args)
        {
            getTestCode();
        }

        public static Assembly getTestCode()
        {
            CompilerParameters CompilerOptions = new CompilerParameters(
                assemblyNames: new String[] { "mscorlib.dll", "System.dll", "System.Core.dll" }, 
                outputName: "test.dll", 
                includeDebugInformation: false) 
            { TreatWarningsAsErrors = true, WarningLevel = 0, GenerateExecutable = false, GenerateInMemory = true };
            List<String> newList = new List<String>();
            String a = null;
            while(a != "end...")
            {
                a = Console.ReadLine();
                if (!a.Equals( "end..."))
                    newList.Add(a);
            }
            String[] source = { "class Test {static void test() {System.Console.WriteLine(\"test\");}}" };
            source = newList.ToArray();
            CSharpCodeProvider zb = new CSharpCodeProvider(new Dictionary<String, String> { { "CompilerVersion", "v4.0" } });
            CompilerResults Results = zb.CompileAssemblyFromSource(CompilerOptions, source);
            Console.WriteLine(Results.Errors.HasErrors);
            CompilerErrorCollection errs = Results.Errors;
            foreach(CompilerError z in errs) 
            {
                Console.WriteLine(z.ErrorText);
            }
            if (!(errs.Count > 0)) 
            {
                AssemblyName assemblyRef = Results.CompiledAssembly.GetName();
                AppDomain.CurrentDomain.Load(assemblyRef);
                //foreach (String a in )
                Console.WriteLine(Results.CompiledAssembly.FullName.ToString());
                Type tempType = Results.CompiledAssembly.GetType("Test");
                MethodInfo tempMethodInfo = tempType.GetMethod("test", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
                if (tempMethodInfo != null)
                    tempMethodInfo.Invoke(null,null);
            }
            Console.ReadLine();
            return null;
        }
    }
}

Now as you can see, basically it compiles the following code:

class Test {static void test() {System.Console.WriteLine(\"test\");}}

Which works fine if you enter it like that (without the ") as userinput into the program. But as soon as you insert a line break by pressing enter after one finished line, the compiling breaks with several errors. It seems like it would evaluate each line as own program by giving following statements:

} expected
Expected class, delegate, enum, interface, or struct
A namespace cannot directly contain members such as fields or methods
A namespace cannot directly contain members such as fields or methods
Type or namespace definition, or end-of-file expected
Type or namespace definition, or end-of-file expected

For following input:

class Test 
{
static void test() 
{
System.Console.WriteLine
("test");
}
}

Do I have to break user (custom) entries down to one line then?

Upvotes: 2

Views: 336

Answers (1)

HasaniH
HasaniH

Reputation: 8402

Each line in sources should contain complete source code not a single line of code. Since you're gathering the code line by line into your source array, you'll have to collapse it into a single string then add that string to an array to pass to CompileAssemblyFromSource Try this:

 while (a != "end...")
 {
     a = Console.ReadLine();
     if (!a.Equals("end..."))
         newList.Add(a);
 }

 string code = string.Join("\r\n", newList);
 string[] source = new string[] { code };

Upvotes: 3

Related Questions