動態執行c#代碼 netcore3下可用


主要是使用CSScript這個庫。nuget上可以選netcore版本的。這個庫已經存在很久了。之前在netframework上就用過,不錯。(效率沒測試,只是實現了我需要的功能)

1 引用 using CSScriptLib;

2 調用
CSScript.EvaluatorConfig.Engine = EvaluatorEngine.Roslyn;
var al= Assembly.LoadFrom(@"F:\gitcode\DynamicPlugin\csst\bin\Debug\netcoreapp3.1\Bll.dll");

        dynamic script = CSScript.RoslynEvaluator.ReferenceAssembly("System.ValueTuple")
                                 .ReferenceAssembly(al)
                                 .LoadMethod(@"string  Hello()
                                               {
                                                 
                                                      
                                                       return Bll.op.GetName();
                                               
                                                   
                                               }");

       var a= script.Hello(); //返回wxq

3 bll的代碼(bll是一個新的項目,只有一個文件op.cs)

using System;
using System.Collections.Generic;
using System.Text;

namespace Bll
{
public class op
{

    public static string GetName() {
        return "wxq";
    }

}

}

4 文檔地址 https://www.cs-script.net/cs-script/help-legacy/root/

另github上有源碼。搜索cs-script .

netcore的地址 https://github.com/oleg-shilo/cs-script.core

原始版本 https://github.com/oleg-shilo/cs-script


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM