Biwen.AutoClassGen
1.8.1
dotnet add package Biwen.AutoClassGen --version 1.8.1
NuGet\Install-Package Biwen.AutoClassGen -Version 1.8.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Biwen.AutoClassGen" Version="1.8.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Biwen.AutoClassGen" Version="1.8.1" />
<PackageReference Include="Biwen.AutoClassGen" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Biwen.AutoClassGen --version 1.8.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Biwen.AutoClassGen, 1.8.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Biwen.AutoClassGen&version=1.8.1
#tool nuget:?package=Biwen.AutoClassGen&version=1.8.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Biwen.AutoClassGen
Biwen.AutoClassGen 是一个代码生成工具库,通过源代码生成器(Source Generator)技术自动创建常用代码模式,简化开发流程,提高工作效率。
主要功能
- DTO自动生成:自动从实体类生成DTO对象,并创建映射扩展方法
- 请求对象生成:快速生成具有相同字段的请求对象族,减少重复工作
- AOP装饰器模式:自动实现装饰器模式,无需手动编写大量样板代码
- 依赖注入自动注册:通过简单的特性标记,自动注册服务到DI容器
- 版本信息生成:自动生成程序集版本信息
- 元数据生成:自动生成程序集元数据
- 代码分析与修复:提供多种代码分析规则和自动修复功能
- 枚举描述生成:根据枚举值自动生成描述信息(
Description
,Display
),方便在UI中显示
快速开始
安装
<ItemGroup>
<PackageReference Include="Biwen.AutoClassGen.Attributes" Version="x.x.x" />
<PackageReference Include="Biwen.AutoClassGen" Version="x.x.x" PrivateAssets="all" />
</ItemGroup>
主要功能示例
1. DTO自动生成
通过简单的特性标记,自动生成DTO类和映射方法:
[AutoDto(typeof(User))] // 或使用 C# 11+ 的泛型特性: [AutoDto<User>]
public partial class UserDto
{
// 属性会自动从User类生成
}
2. 装饰器模式自动实现
轻松实现AOP装饰器模式:
[AutoDecor<LoggingDecorator>]
public interface IUserService
{
Task<User> GetUserAsync(int id);
}
3. 依赖注入自动注册
自动注册服务到DI容器:
[AutoInject<IUserService>(ServiceLifetime.Scoped)]
public class UserService : IUserService
{
// 实现代码
}
4. 版本信息自动生成
自动生成程序集版本信息,支持git版本号:
Console.WriteLine($"Version: {XXX.Generated.Version}");
Console.WriteLine($"Version: {XXX.Generated.AssemblyMetadata}");
5. 枚举描述自动生成
[AutoDescription]
public enum UserStatus
{
[Description("正常")]
Normal,
[Description("禁用")]
Disabled,
[Display(Name = "已删除")]
Deleted
}
// 生成的描述信息
Console.WriteLine(UserStatus.Normal.Description()); // 输出: 正常
Console.WriteLine(UserStatus.Deleted.Description()); // 输出: 已删除
详细文档
以下是每个功能的详细使用说明:
代码分析器功能
该库提供了一系列代码分析器,帮助您编写更规范、更高质量的代码:
生成器相关规则
GEN001
: 检查接口继承关系,确保能正确生成实现类GEN011
: 防止生成类与接口名称冲突GEN021
: 建议使用统一的命名空间以提高代码组织性GEN031
: 提示使用[AutoGen]
特性实现自动生成
DTO生成器规则
GEN041
: 检测重复的[AutoDto]
特性标注GEN042
: 禁止在抽象类上使用[AutoDto]
特性GEN044
: 防止[AutoDto]
引用外部程序集的类GEN045
: 确保标记[AutoDto]
的类声明为 partial
装饰器相关规则
GEN043
: 确保被[AutoDecor]
标记的类型具有public访问级别
代码风格规则
GEN050
: 检查文件是否包含必要的头部注释信息GEN051
: 确保异步方法名称以Async结尾GEN052
: 推荐使用C# 10的文件作用域命名空间声明GEN053
: 确保源代码文件使用UTF-8编码
自动代码修复
本库提供多个自动代码修复功能,帮助您快速修正代码问题:
- ✨ 自动添加缺失的文件头部注释
- 🔄 将异步方法名称规范化(添加Async后缀)
- 🎯 自动应用
[AutoGen]
特性 - 🔍 移除重复的特性标注
- 📝 转换为文件作用域命名空间
- 🛠 更多代码修复持续添加中...
使用该库的项目
以下是一些使用Biwen.AutoClassGen的优秀项目:
- Biwen.QuickApi - 快速API开发框架
如果您的项目正在使用本库,欢迎通过PR将您的项目添加到此列表!
参与贡献
欢迎提交Pull Request来改进这个项目!无论是修复bug、添加新功能,还是完善文档,我们都非常感谢您的贡献。
开源协议
本项目采用MIT协议开源,详见 LICENSE 文件。
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.8.1 | 86 | 3 days ago |
1.7.0 | 162 | 13 days ago |
1.6.0 | 155 | 17 days ago |
1.5.6 | 146 | a month ago |
1.5.5 | 126 | 2 months ago |
1.5.4 | 122 | 5 months ago |
1.5.3 | 137 | 5 months ago |
1.5.2 | 105 | 5 months ago |
1.5.1.3 | 133 | 7 months ago |
1.5.0 | 118 | 7 months ago |
1.3.8 | 128 | 8 months ago |
1.3.7.2 | 119 | 8 months ago |
1.3.6 | 119 | 8 months ago |
1.3.3 | 146 | 6/19/2024 |
1.3.2.3 | 260 | 4/10/2024 |
1.2.1.3 | 208 | 1/22/2024 |
1.2.1.2 | 311 | 11/12/2023 |
1.2.0 | 205 | 11/7/2023 |
1.1.0.2 | 189 | 11/5/2023 |
1.0.0.6 | 159 | 11/4/2023 |
1.0.0 | 147 | 10/31/2023 |
提供对复杂类型的DTO生成支持