HKW.FastMember.Generater
1.3.2
dotnet add package HKW.FastMember.Generater --version 1.3.2
NuGet\Install-Package HKW.FastMember.Generater -Version 1.3.2
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="HKW.FastMember.Generater" Version="1.3.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HKW.FastMember.Generater" Version="1.3.2" />
<PackageReference Include="HKW.FastMember.Generater" />
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 HKW.FastMember.Generater --version 1.3.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: HKW.FastMember.Generater, 1.3.2"
#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 HKW.FastMember.Generater@1.3.2
#: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=HKW.FastMember.Generater&version=1.3.2
#tool nuget:?package=HKW.FastMember.Generater&version=1.3.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
HKW.FastMember.Generater
一个后端包包含运行时程序集和 Roslyn 生成器,正常依赖 HKW.FastMember.Abstractions。
泛型访问器
using HKW.FastMember.Abstractions;
using GeneratedTypes = HKW.FastMember.Generater.TypeAccessor;
using GeneratedObjects = HKW.FastMember.Generater.ObjectAccessor;
var user = new User();
TypeAccessor<User> accessor = GeneratedTypes.Create<User>();
accessor[user, "Id"] = 42;
accessor[user, "Name"] = "Alice";
ObjectAccessor<User> bound = GeneratedObjects.Create(user);
Console.WriteLine(bound["Name"]);
if (bound.TryGetValue("Id", out var id))
Console.WriteLine(id);
if (accessor.CreateNewSupported)
user = accessor.CreateNew();
public sealed class User
{
[Ordinal(0)]
public int Id { get; set; }
[Ordinal(1)]
public string? Name { get; set; }
}
ObjectAccessor<T>.Source非空且只读。访问依据编译期的T,不会根据对象的运行时派生类型发现更多成员。GetMembers()返回按名称StringComparer.Ordinal排序的只读描述列表;GetMemberDictionary()返回区分大小写的只读字典。Member提供Name、Type、Ordinal、CanRead、CanWrite,并包含生成器使用的声明身份及读取器有效类型/空值信息;不包含MemberInfo或任意特性实例。object?值会装箱/解箱,不进行任意类型转换。CreateNew仅使用合法的 公开无参构造。抽象类、仅有非公开构造、未满足必需成员约束时CreateNewSupported=false,调用CreateNew抛NotSupportedException。
支持边界
支持可命名、对生成文件可访问的普通类/记录类、嵌套类、封闭泛型类。根类型不支持结构体、接口、object、动态类型/动态语言运行时、开放类型参数、匿名类型/文件局部类型或不可访问类型。
只包含公开实例属性与字段,排除静态成员、常量、索引器、非公开成员。属性的获取器/设置器分别检查是否公开;只读字段与仅可初始化属性不可写。不访问后备字段。
派生层的合格声明优先;派生的私有/静态同名声明不遮掉可访问基类成员。属性仍遵循虚调用分派。引用返回、类引用类型、指针等无法安全用 object 表达的目标成员会触发编译诊断,不静默忽略。
异常约定
| 情形 | 结果 |
|---|---|
| 目标、源或成员名为空 | ArgumentNullException |
| 索引器未知名称/不可读/不可写 | ArgumentOutOfRangeException,参数 name |
| 尝试操作的名称未知/不可执行 | false;TryGet 输出 null |
| 用户获取器/设置器/构造异常 | 原样传播 |
| 值类型错误/不合法解箱 | 原样传播,不由尝试操作吞掉 |
| 没有生成并注册访问器 | 带类型与修复建议的 InvalidOperationException |
| 注册元数据/契约版本冲突 | InvalidOperationException,不静默接受首个不同实现 |
同步与异步读取器
using HKW.FastMember.Generater;
using var reader = ObjectReader.Create(users, "Id", "Name");
while (reader.Read())
Console.WriteLine(reader.GetInt32(0));
await using var asyncReader = ObjectReader.Create(asyncUsers, cancellationToken, "Id", "Name");
while (await asyncReader.ReadAsync(cancellationToken))
Console.WriteLine(asyncReader.IsDBNull(1) ? "<null>" : asyncReader.GetString(1));
users 为 IEnumerable<User>,asyncUsers 为 IAsyncEnumerable<User>。也提供不带创建取消令牌的异步源重载。即使只调用读取器,也会生成完整类型访问器。
列与架构
- 不传列或传空数组:只选可读成员,按
Ordinal数值、再按名称序号比较排序;未标记序号默认-1。 - 显式列保持顺序,允许重复;
GetOrdinal区分大小写并返回首次位置。未知列名抛IndexOutOfRangeException。 - 列数组或名称为空时报
ArgumentNullException;显式未知/不可读列在创建时抛ArgumentOutOfRangeException,不会先取得枚举器。 - 创建读取器和查询架构不枚举源。空源仍可查询完整架构。
- 空值映射
DBNull.Value;Nullable<T>的GetFieldType为T,AllowDBNull=true。非可空值类型为false;引用类型为true(不根据可空引用类型注解改变)。 GetSchemaTable/GetColumnSchema的列名、序号、类型与空值标志一致,IsKey=false。不执行任意类型转换。
生命周期、取消与释放
HasRows准确判断空源,必要时预读并缓存首行;随后的Read/ReadAsync不跳过该行。非空结果读到末尾后仍有HasRows=true;结束唯一结果集后为false。- 读取前/结束后取值、空行对象,以及关闭后的数据/架构/读取操作抛
InvalidOperationException。到达末尾后重复读取为false;NextResult为false。 - 异步源的同步
Read、HasRows、Close/Dispose可能阻塞。建议使用ReadAsync/DisposeAsync;本库使用ConfigureAwait(false)也无法消除用户枚举器捕获同步上下文所导致的死锁风险。 - 创建取消令牌在创建时检查,并传给延迟取得的异步枚举器。每次
ReadAsync(token)在读取前检查该调用的取消令牌;取消不消费已缓存的行。 - 每次读取传入的取消令牌 不能保证取消由另一取消令牌创建且已运行的
MoveNextAsync;不会为此重建枚举器或放弃仍在执行的操作。 - 不支持并行读取或与
Dispose并发。重叠读取会被拒绝。 Close/Dispose/DisposeAsync共用一次性释放所有权,重复调用不会再次释放枚举器;未开始枚举就释放,不会为了释放而创建枚举器。- 枚举异常原样传播,当前行失效;异常之后不继续读取,清理不覆盖原始枚举异常。
单元测试
dotnet test .\HKW.FastMember.Generater.Tests\HKW.FastMember.Generater.Tests.csproj -c Release -p:GeneratePackageOnBuild=false
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- HKW.FastMember.Abstractions (>= 1.3.2)
-
net8.0
- HKW.FastMember.Abstractions (>= 1.3.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.