主要是使用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