首页 > 代码库 > 使用Roslyn动态编译和执行

使用Roslyn动态编译和执行

1. 安装nuget package

 

 

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


使用Roslyn动态编译和执行