使用Roslyn动态编译和执行

1. 安装nuget package

使用Roslyn动态编译和执行_第1张图片

2.使用Roslyn 动态执行

var engine = newRoslyn.Scripting.CSharp.ScriptEngine();
           engine.CreateSession().Execute
               ("System.Console.WriteLine(\"Hello , Roslyn .\");");


3.使用Roslyn 动态编译

var engine = newRoslyn.Scripting.CSharp.ScriptEngine();
           var list = new List<string> {"abc", "bcd","acd"};
           var excutable = engine.CreateSession().
               CompileSubmission<Func<string,bool>>
                ("(m) =>{returnm.StartsWith(\"a\");}").Execute();
           
           var startWitha = list.
                Where(excutable);
 
           foreach (var str in startWitha)
           {
                Console.WriteLine(str);
            }


你可能感兴趣的:(动态编译)