2

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?

1 Answer 1

3

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 };
Sign up to request clarification or add additional context in comments.

2 Comments

cool this works fine :) but it seems rather complicated to me that they require to collapse the code as you don't really know what to expect, but makes sense on the other hand as the object is named CompilerResults, humm.. I will mark you as solution
If you were reading the input from files, each entry in the sources array is meant to be the contents of an entire source file rather than a single line of code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.