GithubHelp home page GithubHelp logo

dotnet-campus / telescope Goto Github PK

View Code? Open in Web Editor NEW
5.0 7.0 1.0 489 KB

Telescope 是一套预编译框架,可以帮助你将耗时的运行时代码迁移到编译期执行,大幅度提升运行时性能

C# 100.00%
dotnet roslyn sourcegenerator

telescope's Introduction

Telescope

Telescope 是一套预编译框架。

中文 English

当项目安装 Telescope 了之后,项目中即可开始书写预编译代码。通过执行这些预编译代码,项目可以在编译期间执行一些平时需要在运行时执行的代码。这种方式能够将耗时的运行时代码迁移到编译期执行,大幅度提升运行时性能。

Package NuGet
Telescope
Telescope.SourceGeneratorAnalyzers

Telescope.SourceGeneratorAnalyzers

使用 SourceGenerator 源代码生成器的 Telescope 版本

可以用来导出指定类型

用法

支持多个不同的导出写法

分部方法式

这是推荐的方法

在分部类里定义分部方法,分部方法标记 dotnetCampus.Telescope.TelescopeExportAttribute 特性,且返回值包括导出条件,如以下写法

internal partial class Program
{
    [dotnetCampus.Telescope.TelescopeExportAttribute()]
    private static partial IEnumerable<(Type type, F1Attribute attribute, Func<Base> creator)> ExportFooEnumerable();
}

以上代码将导出当前项目标记了 F1Attribute 且继承 Base 的所有类型。经过 Telescope 源代码生成器即可生成大概如下的代码

    [global::System.CodeDom.Compiler.GeneratedCode("dotnetCampus.Telescope.SourceGeneratorAnalyzers", "1.0.0")]
    internal partial class Program
    {
        private static partial IEnumerable<(Type type, F1Attribute attribute, Func<Base> creator)> ExportFooEnumerable()
        {
            yield return (typeof(F2), new F1Attribute()
            {
                       
            }, () => new F2());
            yield return (typeof(F3), new F1Attribute()
            {
                       
            }, () => new F3());
        }
    }

高级用法:

可以在 TelescopeExportAttribute 加上 IncludeReference 属性用来导出所有引用程序集的满足条件的类型,如以下代码

internal partial class Program
{
    [dotnetCampus.Telescope.TelescopeExportAttribute(IncludeReference = true)]
    private static partial IEnumerable<(Type type, F1Attribute attribute, Func<Base> creator)> ExportFooEnumerable();
}

仅推荐在入口程序集加上 IncludeReference = true 属性,因为一旦加入此属性,任何引用程序集的变更都可能导致源代码生成器重复执行,降低 VisualStudio 性能

程序集标记

这是传统的 Telescope 实现方法,在需要导出类型的项目里标记 dotnetCampus.Telescope.MarkExportAttribute 特性,如以下代码

[assembly: dotnetCampus.Telescope.MarkExportAttribute(typeof(Base), typeof(FooAttribute))]

标记之后将会自动生成 dotnetCampus.Telescope.__AttributedTypesExport__ 类型,即可在代码里面直接使用,如以下代码

        var attributedTypesExport = new __AttributedTypesExport__();
        ICompileTimeAttributedTypesExporter<Base, FooAttribute> exporter = attributedTypesExport;
        foreach (var exportedTypeMetadata in exporter.ExportAttributeTypes())
        {
            // 输出导出的类型
            Console.WriteLine(exportedTypeMetadata.RealType.FullName);
        }

也可以使用 dotnetCampus.Telescope.AttributedTypes 辅助类获取所有导出类型

为此项目开发

非常期望你能加入到 Telescope 的开发中来,请阅读 如何为 Telescope 贡献代码 了解开发相关的约定和技术要求。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.