当前位置: 首页 > news >正文

C#动态代码最近实践

使用动态代码后发现内存暴涨, 修复后,最近实践代码如下:
// 关键是这里, 实现了baseScript复用, 否则每次调用内存都会大幅增长, 容易奔溃
var newScript = baseScript.ContinueWith(code);

usingMicrosoft.CodeAnalysis.CSharp.Scripting;usingMicrosoft.CodeAnalysis.Scripting;publicclassDynamicCode{// 创建脚本选项,添加引用和导入(按需), 使用静态, 复用privatestaticScriptOptionsscriptOptions=ScriptOptions.Default.WithReferences(typeof(List<>).Assembly,typeof(ISqlSugarClient).Assembly,typeof(ZXing.BarcodeFormat).Assembly,typeof(SixLabors.ImageSharp.Image).Assembly,typeof(ZXing.ImageSharp.BarcodeWriter<>).Assembly).WithImports("System","System.Collections.Generic","System.Linq","System.IO","System.Linq.Expressions","System.Threading.Tasks","Newtonsoft.Json.Linq","Yitter.IdGenerator","SqlSugar","Admin.NET.Core","System.Text.RegularExpressions","Newtonsoft.Json","ZXing.Common","ZXing.ImageSharp","SixLabors.ImageSharp","SixLabors.ImageSharp.Formats.Png","SixLabors.ImageSharp.PixelFormats");// 导入命名空间privatestaticScriptbaseScript=CSharpScript.Create("",options:scriptOptions,globalsType:typeof(CSharpScriptGlobals4Print));/// <summary>/// 执行动态C#代码获取数据源/// funBody举例/// var funBody = @"var obj = db.Queryable<BasPrint>().ToList(); return new {list=obj};/// </summary>protectedasyncTask<dynamic>QueryDataWithFunBody(stringfunBody,stringp1,dynamicp2){Log.Warning($"QueryDataWithFunBody p1={p1}");Log.Warning($"QueryDataWithFunBody p2={JSON.Serialize(p2)}");Log.Warning($"QueryDataWithFunBody funBody={funBody}");if(string.IsNullOrWhiteSpace(funBody)){returnnew{};}// 创建脚本运行时,并传入全局变量varnewDb=db.CopyNew();// 创建一个新的数据库连接varutils=newCommonUtils();// 创建脚本运行时,并传入全局变量varglobals=newCSharpScriptGlobals4Print{_db=newDb,utils=utils,_p1_=p1,_p2_=p2,};stringcode=$@"public dynamic Multiply(ISqlSugarClient db, string p1, dynamic p2) {{{funBody}}} Multiply(_db, _p1_, _p2_) ";try{varnewScript=baseScript.ContinueWith(code);// 关键是这里, 实现了baseScript复用, 否则每次调用内存都会大幅增长, 容易奔溃varscriptState=awaitnewScript.RunAsync(globals);// 传入变量, 执行动态代码if(scriptState.ReturnValue!=null){varresult=scriptState.ReturnValue;Log.Warning($"QueryDataWithFunBody data={result}");returnresult;}else{Log.Warning($"QueryDataWithFunBody 没有返回值");returnnull;}}catch(CompilationErrorExceptionex){Log.Error("编译错误[QueryDataWithFunBody]",ex);throwOops.Oh($"编译错误[QueryDataWithFunBody]<br>{ex.Message}");}catch(Exceptionex){Log.Error("执行错误[QueryDataWithFunBody]",ex);throwOops.Oh($"执行错误[QueryDataWithFunBody]<br>{ex.Message}");}}}publicclassCSharpScriptGlobals4Print{publicISqlSugarClient_db;publicCommonUtilsutils;publicstring_p1_;publicdynamic_p2_;}
http://www.jsqmd.com/news/342726/

相关文章:

  • Boss项目:部门新增+部门编辑+部门删除+登录拦截(鉴权)
  • 2026 主流 AI 论文生成工具排行榜(按综合推荐指数)
  • 2026年纯碱厂家最新推荐:阻垢剂纯碱厂家/食品级厂家/60-160目工业级纯碱/印染助剂纯碱厂家/工业级氨水厂家/选择指南 - 优质品牌商家
  • 物种分布曲线的五个矩
  • datagrip升级后破解密码--以后也用不到
  • 数据立方体在智慧城市建设中的关键作用
  • 【RAG技术】- RAG系统调优手段之知识库处理(纯干货,建议收藏!!!)
  • 【PMP】风险管理
  • 【PMP】项目生命周期与组织变革
  • PostgreSQL 16 容器主从流复制
  • 前缀和算法:从一道 LeetCode 题看区间求和优化思想
  • Elasticsearch:使用 Elastic Workflows 构建自动化
  • PPP与PPPoE协议介绍
  • Jina Rerankers 为 Elastic 推理服务(EIS)带来了快速、多语言的重排序能力
  • 低功耗蓝牙怎样音频协商音频能力?PACS(Published Audio Capabilities Service)来助力!!
  • 五种并行处理策略对比调研
  • ceph平台-未及时移除故障osd导致根目录100%问题的故障记录
  • 2026年白酒厂家权威推荐榜:白酒贴牌定制厂家、纯粮白酒厂家推荐、纯粮食白酒厂家、贴牌白酒生产厂家、酱香白酒厂家批发选择指南 - 优质品牌商家
  • 缓存特工队:深入浏览器内部的秘密仓库
  • JAVA安全基础-CC3链
  • 基于Spring Boot的企业网盘的设计与实现(开题报告)
  • AI漫剧怎么赚钱:教你用AI漫剧创作系统制作自己的动漫短剧使用云微AI短剧创作系统
  • 【Azure 环境】获取Azure上资源的创建时间createdTime信息(ARM REST API版本)
  • MySQL 导入资料详细说明
  • 米尔顿·弗里德曼《实证经济学方法论》解读
  • 汉字才是终极“外挂”!碾压英文的千年智慧,在AI时代彻底封神
  • Airlink 协议库:实现设备无缝互联的通信基石
  • 从单模态到多模态:AI原生审核技术的融合创新
  • 大规模语言模型在科学实验设计优化中的应用
  • 法尔斯新闻社1398年波斯语新闻数据集_29万条_多领域分类_完整文本内容_自然语言处理_文本挖掘_机器学习训练数据