Asa.SourceGenerators
1.0.14
dotnet add package Asa.SourceGenerators --version 1.0.14
NuGet\Install-Package Asa.SourceGenerators -Version 1.0.14
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="Asa.SourceGenerators" Version="1.0.14" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Asa.SourceGenerators" Version="1.0.14" />
<PackageReference Include="Asa.SourceGenerators" />
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 Asa.SourceGenerators --version 1.0.14
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Asa.SourceGenerators, 1.0.14"
#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.
#:package Asa.SourceGenerators@1.0.14
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Asa.SourceGenerators&version=1.0.14
#tool nuget:?package=Asa.SourceGenerators&version=1.0.14
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Asa.SourceGenerators
一个强大的C#源代码生成器库,提供自动依赖注入和属性变更通知功能。
📦 安装
dotnet add package Asa.SourceGenerators
✨ 功能特性
🔧 自动依赖注入 (GivenClassGenerator)
为标记了 [GivenClass] 特性的 partial 类自动生成构造函数,实现依赖注入。
基本用法:
[GivenClass]
public partial class MyService
{
private readonly ILogger<MyService> _logger;
private readonly IMyRepository _repository;
// 在 Initialize 方法中进行自定义初始化
partial void Initialize()
{
_logger.LogInformation("MyService 已初始化");
}
}
生成的代码:
public MyService(ILogger<MyService> _logger, IMyRepository _repository)
{
this._logger = _logger;
this._repository = _repository;
Initialize();
}
排除特定字段:
[GivenClass]
public partial class MyService
{
private readonly ILogger<MyService> _logger;
[GivenFieldNoInit] // 此字段不会被自动注入
private readonly IMyRepository _repository;
}
🔄 属性变更通知 (GivenPropertyGenerator)
为标记了 [GivenProperty] 特性的字段自动生成属性,包含完整的变更通知事件。
基本用法:
public partial class MyViewModel
{
[GivenProperty]
private string _name;
[GivenProperty]
private int _age;
// 属性变更事件处理
partial void OnNameChanging(string value)
{
// 名称更改前的验证
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("名称不能为空");
}
partial void OnNameChanged(string value)
{
Console.WriteLine($"名称已更改为: {value}");
}
}
生成的代码:
public string Name
{
get => _name;
set
{
var oldValue = _name;
OnNameChanging(value);
OnNameChanging(oldValue, value);
_name = value;
OnNameChanged(value);
OnNameChanged(oldValue, value);
}
}
// 生成的事件处理方法
partial void OnNameChanging(string value);
partial void OnNameChanging(string oldValue, string newValue);
partial void OnNameChanged(string value);
partial void OnNameChanged(string oldValue, string newValue);
🎯 使用指南
前置条件
- 类必须声明为
partial- 这是源代码生成器的要求 - 使用相应的特性标记 -
[GivenClass]或[GivenProperty]
命名约定
属性生成器支持常见的字段命名约定:
_fieldName→FieldNamem_fieldName→FieldNamefieldName→FieldName
生效提示
重要提示: 如果生成器没有生效,请尝试以下步骤:
- 清理解决方案
- 重新生成项目
- 关闭Visual Studio并重新打开项目
- 确保类和字段都标记为
partial
🛠️ 支持的.NET版本
- .NET Standard 2.0+
- .NET 5/6/7/8/9
- .NET Framework 4.6.1+
- Mono / Xamarin
📋 更新日志
v1.0.14 (最新)
性能优化
- ✨ 优化源代码生成器性能
- 改进 SemanticModel 缓存机制,避免重复创建
- 优化增量生成器管道,提高编译性能
- 减少冗余的语义模型查询
代码质量提升
- 🔧 改进错误处理机制
- 使用精确的位置信息进行错误诊断
- 错误信息精确到具体类名和位置
- 添加完善的空值检查,避免运行时异常
- 📝 改进特性匹配逻辑
- 使用
StringComparison.Ordinal进行精确字符串比较 - 避免误匹配特性名称
- 统一代码规范(使用
string.Empty替代"")
- 使用
- 🎯 方法重命名
GivenClassGenerator:GeneratePropertyName→GenerateParameterName(提高可读性)
- 📖 完善文档注释
- 为所有公共方法添加详细的中文 XML 注释
- 说明增量生成器的缓存机制和性能优势
技术亮点
- 增量生成:只处理变更的节点,避免全量重新生成
- 智能缓存:SemanticModel 由增量生成器自动缓存
- 精确诊断:错误信息精确到具体类名和位置
- 类型安全:编译时检查,零运行时开销
v1.0.12
- 🐛 修复命名空间查找问题
- 🔧 改进属性匹配逻辑
- 📊 增强诊断信息
🎯 技术架构
增量生成器优势
本项目的两个生成器都实现了 IIncrementalGenerator 接口,相比传统的 ISourceGenerator 具有以下优势:
- 更高的性能 - 只处理变更的代码节点
- 智能缓存 - 自动缓存未修改的语法树和语义模型
- 更快的编译 - 增量编译时显著减少重新生成的工作量
- 更好的扩展性 - 支持大型代码库而不影响编译速度
生成器工作流程
源代码 → 语法分析 → 语义分析 → 代码生成 → 编译集成
↓ ↓ ↓ ↓ ↓
增量管道 缓存节点 缓存模型 生成代码 输出程序集
📞 支持
- 📧 提交问题: GitHub Issues
- 📖 查看文档: Wiki
- 💬 讨论交流: GitHub Discussions
🔗 相关链接
| 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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
- IndexRange (>= 1.1.0)
- Microsoft.CodeAnalysis.CSharp (>= 5.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Asa.SourceGenerators:
| Package | Downloads |
|---|---|
|
Asa.RabbitMq
扩展rabbitmq,并添加到mongodb记录 |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.14 | 211 | 1/4/2026 |
| 1.0.13 | 315 | 5/6/2025 |
| 1.0.12 | 232 | 5/6/2025 |
| 1.0.11 | 325 | 4/14/2025 |
| 1.0.10 | 236 | 3/27/2025 |
| 1.0.9 | 245 | 3/27/2025 |
| 1.0.8 | 235 | 11/1/2024 |
| 1.0.7 | 205 | 11/1/2024 |
| 1.0.6 | 229 | 10/21/2024 |
| 1.0.5 | 241 | 9/20/2024 |
| 1.0.4 | 245 | 8/30/2024 |
| 1.0.3 | 235 | 8/30/2024 |
| 1.0.2 | 265 | 8/12/2024 |
| 1.0.1 | 234 | 8/12/2024 |
| 1.0.0 | 227 | 8/12/2024 |