Telescope 是一套基于 Roslyn 增量源生成器的预编译框架。
| 中文 | English |
|---|
当项目安装 DotNetCampus.Telescope NuGet 包后,即可在编译期自动扫描并导出程序集中的类型与特性元数据,将耗时的运行时反射迁移到编译期执行,大幅度提升应用程序启动与运行性能。
| Package | NuGet |
|---|---|
| DotNetCampus.Telescope |
支持的目标框架:net6.0、netstandard2.0、net45。
<PackageReference Include="DotNetCampus.Telescope" Version="1.0.0" />DotNetCampus.Telescope 单个 NuGet 包同时包含运行时类型与 Roslyn 增量源生成器,不再需要分别安装 TelescopeSource 或独立的分析器包。
支持两种导出写法:
在分部类里定义分部方法,标记 [TelescopeExport] 特性,并在返回值元组中声明导出条件:
using DotNetCampus.Telescope;
internal partial class Program
{
[TelescopeExport]
private static partial IEnumerable<(Type type, F1Attribute attribute, Func<Base> creator)> ExportFooEnumerable();
}以上代码会在编译期导出当前项目中标记了 F1Attribute 且继承自 Base 的所有非抽象类型,源生成器会自动补全该分部方法的实现:
internal partial class Program
{
private static partial global::System.Collections.Generic.IEnumerable<(global::System.Type type, global::Demo.F1Attribute attribute, global::System.Func<global::Demo.Base> creator)> ExportFooEnumerable()
{
yield return (typeof(global::Demo.F2), new global::Demo.F1Attribute(), () => new global::Demo.F2());
yield return (typeof(global::Demo.F3), new global::Demo.F1Attribute(), () => new global::Demo.F3());
}
}如果不限制特性,只需导出继承自 Base 的所有类型,可省略元组中的特性项:
[TelescopeExport]
private static partial IEnumerable<(Type type, Func<Base> creator)> ExportAllSubTypes();在 TelescopeExportAttribute 上设置 IncludeReferences = true 可一并导出所有引用程序集中满足条件的类型:
internal partial class Program
{
[TelescopeExport(IncludeReferences = true)]
private static partial IEnumerable<(Type type, F1Attribute attribute, Func<Base> creator)> ExportFooEnumerable();
}仅推荐在入口程序集启用 IncludeReferences = true,因为任何引用程序集的变更都会触发源生成器重新执行。
在需要导出类型的项目中添加程序集级 [assembly: MarkExport(...)] 特性:
using DotNetCampus.Telescope;
[assembly: MarkExport(typeof(Base), typeof(FooAttribute))]源生成器会在编译期自动生成 DotNetCampus.Telescope.__AttributedTypesExport__,运行时可通过 AttributedTypes.FromAssembly 跨程序集读取:
using DotNetCampus.Telescope;
var exportedTypes = AttributedTypes.FromAssembly<Base, FooAttribute>(typeof(Program).Assembly);
foreach (var metadata in exportedTypes)
{
Console.WriteLine(metadata.RealType.FullName);
var instance = metadata.CreateInstance();
}非常期望你能加入到 Telescope 的开发中来,请阅读 如何为 Telescope 贡献代码 了解开发相关的约定和技术要求。