动态执行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

posted @ 2020-01-08 15:20  过错  阅读(1344)  评论(0)    收藏  举报