news 2026/4/15 20:34:39

.Net如何自定义优雅实现代码生成器

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
.Net如何自定义优雅实现代码生成器

需求分析与场景定义

  • 明确代码生成器的目标:生成实体类、API控制器、DTO等常见场景
  • 典型应用场景:快速开发CRUD功能、减少重复编码工作
  • 核心需求:可配置性、模板灵活性、与项目结构无缝集成

具体实现可参考NetCoreKevin的kevin.CodeGenerator模块

基于.NET构建的企业级SaaS智能应用架构,采用前后端分离设计,具备以下核心特性:
前端技术:

  • Vue3前端框架
  • IDS4单点登录系统
  • 一库多租户解决方案
  • 多级缓存机制
  • CAP事件集成
  • SignalR实时通信
  • 领域驱动设计
  • AI智能体框架
  • RabbitMQ消息队列
  • 项目地址:github:https://github.com/junkai-li/NetCoreKevin
    Gitee: https://gitee.com/netkevin-li/NetCoreKevin

第一步:配置模板

模板配置示例如下图所示:

创建kevin.CodeGenerator模块

ICodeGeneratorService接口定义

usingkevin.CodeGenerator.Dto;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacekevin.CodeGenerator{publicinterfaceICodeGeneratorService{/// <summary>/// 获取区域名称列表/// </summary>/// <returns></returns>Task<List<string>>GetAreaNames();/// <summary>/// 获取区域名称下面的表列表/// </summary>/// <returns></returns>Task<List<EntityItemDto>>GetAreaNameEntityItems(stringareaName);/// <summary>/// 生成代码/// </summary>/// <param name="entityItems"></param>/// <returns></returns>Task<bool>BulidCode(List<EntityItemDto>entityItems);}}

CodeGeneratorService实现

usingkevin.CodeGenerator.Dto;usingMicrosoft.CodeAnalysis;usingMicrosoft.CodeAnalysis.CSharp;usingMicrosoft.CodeAnalysis.CSharp.Syntax;usingMicrosoft.Extensions.Options;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Reflection.Metadata;usingSystem.Text;usingstaticMicrosoft.CodeAnalysis.CSharp.SyntaxTokenParser;namespacekevin.CodeGenerator{publicclassCodeGeneratorService:ICodeGeneratorService{privateCodeGeneratorSetting_config;publicCodeGeneratorService(IOptionsMonitor<CodeGeneratorSetting>config){_config=config.CurrentValue;}publicasyncTask<List<string>>GetAreaNames(){return_config.CodeGeneratorItems.Select(t=>t.AreaName).ToList();}publicasyncTask<List<EntityItemDto>>GetAreaNameEntityItems(stringareaName){vararea=_config.CodeGeneratorItems.FirstOrDefault(t=>t.AreaName==areaName);if(area!=default){varentityItems=newList<EntityItemDto>();varpath="..\\..\\"+area.AreaPath.Trim().Replace(".","\\");// 遍历路径下的所有 .cs 文件if(!Directory.Exists(path)){thrownewArgumentException($"CodeGeneratorSetting配置:{areaName}{area.AreaPath}不存在");}else{varcsFiles=Directory.GetFiles(path,"*.cs",SearchOption.AllDirectories);foreach(varfileincsFiles){// 读取文件内容varcode=File.ReadAllText(file);vartree=CSharpSyntaxTree.ParseText(code);varroot=(CompilationUnitSyntax)tree.GetRoot();// 查找所有类声明varclassDeclarations=root.DescendantNodes().OfType<ClassDeclarationSyntax>();foreach(varclassDeclarationinclassDeclarations){// 检查类是否有 Table 特性if(classDeclaration.AttributeLists.Any(list=>list.Attributes.Any(attr=>attr.Name.ToString()=="Table"))){stringdescription="";// 检查类是否有 Description 特性vardescriptionAttr=classDeclaration.AttributeLists.SelectMany(list=>list.Attributes).FirstOrDefault(attr=>attr.Name.ToString()=="Description");if(descriptionAttr!=null){// 获取特性参数值vararg=descriptionAttr.ArgumentList?.Arguments.FirstOrDefault();if(arg?.ExpressionisLiteralExpressionSyntaxliteral){description=literal.Token.ValueText;}}entityItems.Add(newEntityItemDto{AreaName=area.AreaName,EntityName=classDeclaration.Identifier.Text,Description=$"{file}:{description}"});}}}returnentityItems;}}returnnewList<EntityItemDto>();}publicasyncTask<bool>BulidCode(List<EntityItemDto>entityItems){//获取对应的模板文件variRpTemplate=GetBuildCodeTemplate("IRp");varrpTemplate=GetBuildCodeTemplate("Rp");variServiceTemplate=GetBuildCodeTemplate("IService");varservice=GetBuildCodeTemplate("Service");foreach(variteminentityItems){vararea=_config.CodeGeneratorItems.FirstOrDefault(t=>t.AreaName==item.AreaName);if(area!=default){if(item.EntityName.StartsWith("T",StringComparison.OrdinalIgnoreCase)){item.EntityName=item.EntityName.Substring(1);}WriteCode(newDictionary<string,string>{{"%entityName%",item.EntityName},{"%namespacePath%",area.IRpBulidPath}},iRpTemplate,$"../../{area.IRpBulidPath.Trim().Replace(".","\\")}/I{item.EntityName}Rp.cs");WriteCode(newDictionary<string,string>{{"%entityName%",item.EntityName},{"%namespacePath%",area.RpBulidPath}},rpTemplate,$"../../{area.RpBulidPath.Trim().Replace(".","\\")}/{item.EntityName}Rp.cs");WriteCode(newDictionary<string,string>{{"%entityName%",item.EntityName},{"%namespacePath%",area.IServiceBulidPath}},iServiceTemplate,$"../../{area.IServiceBulidPath.Trim().Replace(".","\\")}/I{item.EntityName}Service.cs");WriteCode(newDictionary<string,string>{{"%entityName%",item.EntityName},{"%namespacePath%",area.ServiceBulidPath}},service,$"../../{area.ServiceBulidPath.Trim().Replace(".","\\")}/{item.EntityName}Service.cs");}}returntrue;}/// <summary>/// 获取对应模板文件/// </summary>/// <param name="name"></param>/// <returns></returns>privatestringGetBuildCodeTemplate(stringname){returnFile.ReadAllText("..\\..\\"+"Kevin\\kevin.Module\\kevin.CodeGenerator\\BuildCodeTemplate\\"+name+".txt",encoding:Encoding.UTF8);}/// <summary>/// 生成文件和代码/// </summary>/// <param name="paramters"></param>/// <param name="content"></param>/// <param name="savePath"></param>privatevoidWriteCode(Dictionary<string,string>paramters,stringcontent,stringsavePath){foreach(variteminparamters){content=content.Replace(item.Key,item.Value);}vardir=Path.GetDirectoryName(savePath);if(!Directory.Exists(dir)){Directory.CreateDirectory(dir);}if(File.Exists(savePath)){Console.WriteLine($"文件{savePath}已存在,跳过生成!");}else{File.WriteAllText(savePath,content,Encoding.UTF8);}}}}

CodeGeneratorSettingDto

namespacekevin.CodeGenerator.Dto{publicclassCodeGeneratorSetting{/// <summary>/// 配置文件相关信息/// </summary>publicList<CodeGeneratorItem>CodeGeneratorItems{get;set;}=new();}publicclassCodeGeneratorItem{/// <summary>/// 区域/// </summary>publicstringAreaName{get;set;}="";/// <summary>/// 数据库实体类路径/// </summary>publicstringAreaPath{get;set;}="";/// <summary>/// 仓储接口生成路径/// </summary>publicstringIRpBulidPath{get;set;}="";/// <summary>/// 仓储生成路径/// </summary>publicstringRpBulidPath{get;set;}="";/// <summary>/// 服务接口生成路径/// </summary>publicstringIServiceBulidPath{get;set;}="";/// <summary>/// 服务生成路径/// </summary>publicstringServiceBulidPath{get;set;}="";}}

配置Json文件

////代码生成器配置 .转换成/时要和路径一致 请配置好命名空间和路径对应关系"CodeGeneratorSetting":{"CodeGeneratorItems":[{"AreaName":"App.WebApi.v1",//项目命名"AreaPath":"App.Domain.Entities",//实体类路径"IRpBulidPath":"App.Domain.Interfaces.Repositorie.v1",//仓储接口命名空间和路径"RpBulidPath":"App.RepositorieRps.Repositories.v1",//仓储命名空间和路径"IServiceBulidPath":"App.Domain.Interfaces.Services.v1",//服务接口命名空间和路径"ServiceBulidPath":"App.Application.Services.v1"//服务命名空间和路径}]}

服务注入

services.AddKevinCodeGenerator(options=>{varsettings=Configuration.GetRequiredSection("CodeGeneratorSetting").Get<CodeGeneratorSetting>()!;options.CodeGeneratorItems=settings.CodeGeneratorItems;});

使用

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 11:10:36

TEKLauncher终极指南:ARK生存进化游戏管理革命

TEKLauncher终极指南&#xff1a;ARK生存进化游戏管理革命 【免费下载链接】TEKLauncher Launcher for ARK: Survival Evolved 项目地址: https://gitcode.com/gh_mirrors/te/TEKLauncher 还在为ARK: Survival Evolved的MOD冲突而头疼吗&#xff1f;每次和朋友联机都要折…

作者头像 李华
网站建设 2026/4/16 14:00:51

PDFView安卓PDF查看器终极使用指南:解决你的文档阅读烦恼

PDFView安卓PDF查看器终极使用指南&#xff1a;解决你的文档阅读烦恼 【免费下载链接】PDFView 安卓PDF查看器&#xff0c;自定义View实现。支持添加水印、三级缓存、页面预加载&#xff0c;缩放查看高清。 项目地址: https://gitcode.com/gh_mirrors/pd/PDFView 还在为…

作者头像 李华
网站建设 2026/4/16 10:42:53

城市交通拥堵分析:基于报告提取高峰时段规律

城市交通拥堵分析&#xff1a;从报告中挖掘高峰规律的智能路径 在早晚高峰挤在车流中动弹不得时&#xff0c;你是否想过&#xff1a;这些拥堵真是不可避免吗&#xff1f;城市管理者又是否真的掌握了每一条道路的“呼吸节奏”&#xff1f;事实上&#xff0c;大量关于交通运行的数…

作者头像 李华
网站建设 2026/4/16 11:00:19

DBAN数据擦除工具:手把手教你彻底清理硬盘数据

DBAN数据擦除工具&#xff1a;手把手教你彻底清理硬盘数据 【免费下载链接】dban Unofficial fork of DBAN. 项目地址: https://gitcode.com/gh_mirrors/db/dban 还在担心旧电脑里的隐私数据泄露吗&#xff1f;&#x1f914; 今天就来给大家介绍一款专业的硬盘数据擦除工…

作者头像 李华
网站建设 2026/4/16 12:26:37

终极解决方案:在Windows系统上直接安装安卓应用

还在为电脑和手机之间的应用壁垒而烦恼吗&#xff1f;现在&#xff0c;一款革命性的工具彻底改变了跨平台应用部署的游戏规则&#xff01;APK Installer作为专为Windows设计的安卓应用安装器&#xff0c;让你无需依赖笨重的模拟器&#xff0c;就能在电脑上轻松管理和安装APK文件…

作者头像 李华
网站建设 2026/4/16 13:01:49

BG3ModManager高效实用指南:博德之门3模组管理完整教程

BG3ModManager高效实用指南&#xff1a;博德之门3模组管理完整教程 【免费下载链接】BG3ModManager A mod manager for Baldurs Gate 3. 项目地址: https://gitcode.com/gh_mirrors/bg/BG3ModManager 想要在《博德之门3》中畅享模组带来的丰富体验&#xff0c;却苦于模组…

作者头像 李华