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