Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用CodeDom编译_C#_Compilation_Codedom - Fatal编程技术网

C# 用CodeDom编译

C# 用CodeDom编译,c#,compilation,codedom,C#,Compilation,Codedom,我开始尝试使用CodeDom,并制作了一个简单的应用程序,从用户输入中收集源代码,并尝试用C#语法编译它 对于那些想要尝试整个过程的人,键入end…以完成源代码条目 下面是一个例子: using System; using System.Collections; using System.Reflection; using System.Collections.Generic; using System.Diagnostics; using Microsoft.CSharp; using Sys

我开始尝试使用CodeDom,并制作了一个简单的应用程序,从用户输入中收集源代码,并尝试用C#语法编译它

对于那些想要尝试整个过程的人,键入end…以完成源代码条目

下面是一个例子:

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;
        }
    }
}
如果您这样输入它(不使用“)作为用户输入到程序中,它可以正常工作。但是,只要在一行完成后按enter键插入换行符,编译就会中断并出现多个错误。它似乎会通过给出以下语句将每一行作为自己的程序进行计算:

} 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
对于以下输入:

class Test 
{
static void test() 
{
System.Console.WriteLine
("test");
}
}
是否必须中断用户(自定义)然后将条目压缩到一行?

源代码中的每一行都应该包含完整的源代码,而不是一行代码。由于您要将代码逐行收集到源代码数组中,因此必须将其压缩为单个字符串,然后将该字符串添加到数组中,以传递到
compileasemblyfromsource
试试这个:

 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 };

酷这个很好:)但对我来说,他们需要折叠代码似乎相当复杂,因为你真的不知道会发生什么,但从另一方面来说是有意义的,因为对象名为CompilerResults,哼哼..我会将你标记为解决方案如果你从文件中读取输入,源数组中的每个条目都是内容一个完整的源文件,而不是一行代码
 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 };