Wen.Property.Generator.Runtime
2.2.0
dotnet add package Wen.Property.Generator.Runtime --version 2.2.0
NuGet\Install-Package Wen.Property.Generator.Runtime -Version 2.2.0
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="Wen.Property.Generator.Runtime" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Wen.Property.Generator.Runtime" Version="2.2.0" />
<PackageReference Include="Wen.Property.Generator.Runtime" />
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 Wen.Property.Generator.Runtime --version 2.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Wen.Property.Generator.Runtime, 2.2.0"
#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 Wen.Property.Generator.Runtime@2.2.0
#: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=Wen.Property.Generator.Runtime&version=2.2.0
#tool nuget:?package=Wen.Property.Generator.Runtime&version=2.2.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Wen.Property.Generator
一个基于 C# Roslyn 增量源生成器 的零反射属性访问库。通过编译期代码生成,为实现了 IWenPropertyAccessor 接口的类自动生成强类型属性读写实现,无需反射、AOT 友好、零运行时开销。
特性
- 零反射 — 所有属性访问均为编译期生成的直接调用,无
System.Reflection - AOT 友好 — 无动态代码生成,完全兼容 Native AOT / Trimming
- 增量生成器 — 基于
IIncrementalGenerator,仅对变更的类重新生成,编译速度快 - 强类型泛型 API — 提供
GetPropertyValue<T>/SetPropertyValue<T>避免装箱拆箱 - 只读属性保护 — 对只读属性的写操作在运行时抛出明确异常
- 诊断提示 — 未标记
partial的类会触发WENPROP001编译错误,即时提醒
项目结构
Wen.Property.Generator.slnx
├── Wen.Property.Generator/ # 源生成器(netstandard2.0, Roslyn Analyzer)
├── Wen.Property.Generator.Runtime/ # 运行时接口库(IWenPropertyAccessor)
└── Wen.Property.Generator.Test/ # 示例 / 测试项目
安装
通过 NuGet 安装生成器包(会自动携带 Runtime 依赖):
dotnet add package Wen.Property.Generator
快速开始
1. 定义类并实现接口
using Wen.Property.Generator;
public partial class MyClass : IWenPropertyAccessor
{
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
public DateTime CreatedAt { get; set; }
public string? Remark { get; } // 只读属性
}
注意: 类必须声明为
partial,否则触发WENPROP001编译错误。
2. 使用生成的 API
var obj = new MyClass { Name = "Alice", Age = 30, CreatedAt = DateTime.Now };
// object? 版本(值类型会装箱)
object? name = obj.GetPropertyValue("Name");
obj.SetPropertyValue("Age", 31);
// 强类型泛型版(无装箱拆箱)
int age = obj.GetPropertyValue<int>("Age");
obj.SetPropertyValue<string>("Name", "Bob");
// 判断属性是否存在
bool exists = obj.ContainsProperty("Name"); // true
bool nope = obj.ContainsProperty("Foo"); // false
// 获取属性类型
Type t = obj.GetPropertyType("CreatedAt"); // typeof(DateTime)
// 只读属性写入会抛出 ArgumentException
obj.SetPropertyValue("Remark", "x"); // throws!
IWenPropertyAccessor 接口
public interface IWenPropertyAccessor
{
object? GetPropertyValue(string propertyName);
void SetPropertyValue(string propertyName, object? value);
TValue GetPropertyValue<TValue>(string propertyName);
void SetPropertyValue<TValue>(string propertyName, TValue value);
Type GetPropertyType(string propertyName);
bool ContainsProperty(string propertyName);
}
生成规则
| 条件 | 说明 |
|---|---|
类必须为 partial |
否则报 WENPROP001 错误 |
| 仅处理非泛型、非嵌套类 | 泛型类 / 嵌套类会被跳过 |
仅包含 public 实例属性 |
静态属性、索引器、显式接口实现均被排除 |
| 只读属性 | 生成 getter,setter 抛出 ArgumentException |
环境要求
- .NET SDK 6.0+(支持 Roslyn 4.x 增量生成器)
- C# 10+
- 目标框架:
netstandard2.0/net6.0/net8.0等均可
许可证
MIT
| 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
- No dependencies.
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Wen.Property.Generator.Runtime:
| Package | Downloads |
|---|---|
|
Wen.Property.Generator
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.2.0 | 108 | 7/24/2026 |