如何在CodeDom中使用Lambda?

时间:2017-05-03 23:55:42

标签: c# codedom

CodeDom拒绝允许我传递此功能。它会引发各种错误。

using System.Linq;

INode root = nodes.Single(n => n.Type == NodeType.Root);

我的编译器代码:

public static bool CompileFromSource(string Output, string source, string Icon = null, string[] Resources = null)
{
    CompilerParameters CParams = new CompilerParameters();

    CParams.GenerateExecutable = true;
    CParams.OutputAssembly = Output;

    string options = "/optimize+ /platform:x86 /target:winexe /unsafe";

    if (Icon != null)
        options += " /win32icon:\"" + Icon + "\"";

    CParams.CompilerOptions = options;

    CParams.TreatWarningsAsErrors = false;
    CParams.ReferencedAssemblies.Add("System.dll");
    CParams.ReferencedAssemblies.Add("System.Linq.dll");
    CParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
    CParams.ReferencedAssemblies.Add("System.Data.dll");
    CParams.ReferencedAssemblies.Add("MegaApiClient.dll");

    if (Resources != null && Resources.Length > 0)
    {
        // Loop through all resource files specified in the Resources[] array.
        foreach (string res in Resources)
        {
            // Add each resource file to the compiled stub.
            CParams.EmbeddedResources.Add(res);
        }
   }

   Dictionary<string, string> ProviderOptions = new Dictionary<string, string>();
   ProviderOptions.Add("CompilerVersion", "v2.0");

   CompilerResults Results = new CSharpCodeProvider(ProviderOptions).CompileAssemblyFromSource(CParams, source);

   if (Results.Errors.Count > 0)
   {
       MessageBox.Show(string.Format("The compiler has encountered {0} errors", Results.Errors.Count), "Errors while compiling", MessageBoxButtons.OK, MessageBoxIcon.Error);

       foreach (CompilerError Err in Results.Errors)
       {
           MessageBox.Show(string.Format("{0}\nLine: {1} - Column: {2}\nFile: {3}", Err.ErrorText, Err.Line, Err.Column, Err.FileName), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
       }

       return false;
   }
   else
   {
       return true;
   }
}

显然lambda讨厌在CodeDom中编译。什么相当于这个,所以我可以使用它?或者我如何在CodeDom中编译它?

INode root = nodes.Single(n => n.Type == NodeType.Root);

0 个答案:

没有答案