惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LINUX DO - 最新话题
G
Google Developers Blog
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
F
Full Disclosure
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News
酷 壳 – CoolShell
酷 壳 – CoolShell
Recent Announcements
Recent Announcements
Help Net Security
Help Net Security
The Hacker News
The Hacker News
IT之家
IT之家
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
V
Visual Studio Blog
博客园 - 聂微东
Hacker News: Ask HN
Hacker News: Ask HN
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Security Latest
Security Latest
Security Archives - TechRepublic
Security Archives - TechRepublic
Simon Willison's Weblog
Simon Willison's Weblog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Last Week in AI
Last Week in AI
Schneier on Security
Schneier on Security
N
News and Events Feed by Topic
博客园 - 【当耐特】
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
Google DeepMind News
Google DeepMind News
Cloudbric
Cloudbric
N
News | PayPal Newsroom
A
About on SuperTechFans
S
Schneier on Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
罗磊的独立博客
K
Kaspersky official blog
The Cloudflare Blog
I
Intezer

博客园 - HackerVirus

在window系统下搭建C/C++开发环境 C#中DataGridView处理大数据量的技巧分享 CSharp-MVVM框架 多线程&线程池 Prism的事件聚合器 Toggling Focus Assist mode in Win 10 Programmatically Windsurf:超越 Cursor 的下一代 AI 编辑器 编程辅助新选择:Devin与Cursor的性能对比评测 支持多语言、多商店的商城,.Net7 + EF7领域驱动设计架构 Mysql高可用架构方案 软件架构中的那些惯用手段 .Net轻量级的CMS开源项目 前后端分离的权限管理框架,前端采用 Vue 3 框架,后端采用 .NET 8、ORM 采用 EF 8 linux文件属性 IHostedService 和 BackgroundService 区别 Uno Platform是一个基于C#开源、功能强大、灵活的跨平台开发框架,用于快速构建单一代码库原生移动、Web、桌面和嵌入式应用程序 使用HslCommunication类库读取Siemens PLC DATA 功能齐全的 WPF 自定义控件 Sprint Planning
Roslyn的源生成器
HackerVirus · 2025-01-08 · via 博客园 - HackerVirus

使用Roslyn的源生成器生成DTO

前言

源生成器的好处很多, 通过在编译时生成代码,可以减少运行时的反射和动态代码生成,从而提高应用程序的性能, 有时候需要对程序AOT以及裁剪编译的dll也是需要用SG来处理的。

我们开发程序应该都绕不过Mapper对象映射,用的比较多的库可能就是AutoMapper,Maspter之内的三方库吧;这些库很强大但是因为内部实现存在反射,因此开发的程序就没办法AOT了,因此如果程序不是很复杂但是又有很特殊的需求,建议使用SG来实现Mapper

功能演示

这里我演示下自己开发的AutoDto生成DTO功能:

比如我们有一个User的类,需要生成UserDto

public class User
{
	public string Id { get; set; } = null!;
	public string FirstName { get; set; } = null!;
	public string LastName { get; set; } = null!;
	public int? Age { get; set; }
	public string? FullName => $"{FirstName} {LastName}";
}

定义UserDto并标注特性:

[AutoDto<User>(nameof(User.Id))]//这里我们假设排除Id属性
public partial record UserDto;

就这样,源生成器将自动为我们生成对应的Dto:

partial record class UserDto
{
	

并同时为我们生成一个简单的Mapper扩展方法:

public static partial class UserToUserDtoExtentions
{
	

实现代码

static void GENDTO(Compilation compilation, ImmutableArray<SyntaxNode> nodes, SourceProductionContext context)
{
	if (nodes.Length == 0) return;
	StringBuilder envStringBuilder = new();
	envStringBuilder.AppendLine("// <auto-generated />");
	envStringBuilder.AppendLine("using System;");
	envStringBuilder.AppendLine("using System.Collections.Generic;");
	envStringBuilder.AppendLine("using System.Text;");
	envStringBuilder.AppendLine("using System.Threading.Tasks;");
	envStringBuilder.AppendLine("#pragma warning disable");

	foreach (var nodeSyntax in nodes.AsEnumerable())
	{
		

最后

以上代码就完成了整个源生成步骤,最后你可以使用我发布的nuget包体验:

<ItemGroup>
   <PackageReference Include="Biwen.AutoClassGen.Attributes" Version="1.3.6" />
   <PackageReference Include="Biwen.AutoClassGen" Version="1.5.4" PrivateAssets="all" />
</ItemGroup>

当然如果你对完整的实现感兴趣可以移步我的GitHub仓储,欢迎star https://github.com/vipwan/Biwen.AutoClassGen

本文版权归作者所有,转载请注明出处!