CyreneMetadata 1.1.0

dotnet add package CyreneMetadata --version 1.1.0
                    
NuGet\Install-Package CyreneMetadata -Version 1.1.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="CyreneMetadata" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CyreneMetadata" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="CyreneMetadata" />
                    
Project file
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 CyreneMetadata --version 1.1.0
                    
#r "nuget: CyreneMetadata, 1.1.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 CyreneMetadata@1.1.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=CyreneMetadata&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=CyreneMetadata&version=1.1.0
                    
Install as a Cake Tool

CyreneMetadata

一个编译期元数据库,通过源代码生成器收集派生类型、属性和构造函数信息,替代运行时反射。

NuGet License

特性

  • 零反射 - 派生类型、属性、构造函数在编译期收集并生成静态注册代码,运行时直接查表
  • 按需生成 - 只有代码里实际调用过的类型才会被收集,不产生多余元数据
  • AOT 兼容 - 完全支持 Native AOT 与裁剪,不触发 trimmer 警告
  • 自动注册 - 通过 [ModuleInitializer] 在程序启动时自动完成注册,无需手动初始化

安装

dotnet add package CyreneMetadata

快速开始

直接调用 CyreneMetadataCache 的查询方法,生成器会识别调用中 typeof(...) 的根类型并在编译期收集其元数据。参数必须是编译期已知的字面 typeof,不能传运行时 Type 变量。

派生类型

传入基类或接口,获取程序集内所有可访问的派生类型:

using CyreneMetadata;

Type[] shapes = CyreneMetadataCache.GetDerive(typeof(Shape));

foreach (var type in shapes)
{
    var shape = (Shape)CyreneMetadataCache.CreateInstance(type); // 编译期生成的无参构造工厂
}
  • 只收集有 public 无参构造函数的类型才可用于 CreateInstance
  • 支持未绑定泛型基类(如 typeof(Repository<>))

带参构造

CreateInstance(Type, params object?[] args) 接受任意数量的构造参数。生成器根据调用处各实参的静态类型确定构造签名,并为所有已通过 GetDerive 收集、且拥有匹配 public 构造函数的派生类型生成工厂:

using CyreneMetadata;

foreach (var type in CyreneMetadataCache.GetDerive(typeof(BaseHandler)))
{
    // 生成 static a => new XxxHandler((Connection)a[0]!, (PacketData)a[1]!)
    var handler = (BaseHandler)CyreneMetadataCache.CreateInstance(type, connection, packet);
}
  • 实参必须在调用处逐个列出,或写成集合/数组字面量(CreateInstance(type, [a, b]));传入数组变量会报 CYMT004
  • 实参类型与构造参数一致,或可隐式引用转换/装箱时匹配;数值转换(如 int → long)与自定义转换不匹配
  • null 字面量匹配任意可为 null 的参数;不支持 ref / out / in 参数
  • 运行时按参数个数与实参的实际类型选择构造函数,没有匹配项时抛出 ArgumentException
  • 只为实际出现过的签名生成,不会为每个类型的所有构造函数生成工厂

属性元数据

传入类型,获取其属性列表(含基类属性),并通过委托读写:

using CyreneMetadata;

CyrenePropertyInfo[] props = CyreneMetadataCache.GetProperties(typeof(User));

var user = new User();
foreach (var prop in props)
{
    if (prop.CanWrite && prop.HasAttribute(typeof(RequiredAttribute)))
        prop.Set!(user, defaultValue);

    object? value = prop.CanRead ? prop.Get!(user) : null;
}

GetProperties 支持 BindingFlags 重载,语义与反射一致(Public/NonPublic/Instance/Static/DeclaredOnly/FlattenHierarchy)。

成员 说明
Name / PropertyType / DeclaringType 属性名、类型、声明类型
IsStatic / IsPublic 修饰符信息
Get / Set 读写委托,不可读写时为 null
CanRead / CanWrite 是否可读写
Attributes / HasAttribute(Type) 属性上标注的特性类型

要求

  • .NET 8.0 或更高版本
  • C# 12 或更高版本

许可证

本项目采用 MIT。

链接

Product 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

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.1.0 93 11/13/2026