百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

comptime

> 编程语言
开源

Comptime 为 C# 带来元编程功能,支持编译时代码生成和评估。

573 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Comptime 为 C# 带来元编程功能,支持编译时代码生成和评估。

Comptime

A .NET source generator that executes methods at compile time and serializes their results to C# code. Comptime brings meta-programming capabilities to C#, enabling compile-time code generation and evaluation.

Overview

Comptime allows you to mark methods with the [Comptime] attribute to have them executed during compilation. The return values are serialized into C# source code and used at runtime, eliminating the need for runtime computation of values that can be determined at build time.

This meta-programming approach enables developers to shift expensive computations from runtime to compile time, resulting in faster application startup and execution.

Features

  • Compile-time execution: Methods marked with [Comptime] are executed during compilation
  • Method parameters: Methods can accept parameters with compile-time constant expressions
  • C# serialization: Results are serialized to valid C# code
  • Supported return types:
    • Primitive types: int, long, short, byte, sbyte, uint, ulong, ushort, float, double, decimal, bool, char, string
    • Collections: IReadOnlyList, IReadOnlyDictionary, List, Dictionary
    • Note: Arrays are not allowed as return types because they are mutable. Use IReadOnlyList instead.
  • Supported argument types: Any expression that doesn't contain variables, including:
    • Literals: 42, "hello", true
    • Collection initializers: new List { 1, 2, 3 }, new[] { "a", "b", "c" }
    • Expressions: 1 + 2, Math.PI * 2
    • Const values and enum members
  • Interceptor-based: Uses C# interceptors to replace method calls with pre-computed values

Usage

Basic Usage (Parameterless Methods)

…

Requirements

  • .NET 8.0 or later
  • C# 12 or later (for interceptors support)

Installation

xml

How It Works

  1. The source generator finds methods marked with [Comptime]
  2. It identifies all call sites and their arguments
  3. For each unique argument combination, it executes the method at compile time
  4. The return values are serialized to C# literals/expressions
  5. Interceptor methods are generated that return the pre-computed values
  6. At runtime, calls to the original methods are intercepted and return the cached values

Diagnostics

Code Description
COMPTIME001 Class must be partial
COMPTIME002 Method must be static
COMPTIME004 Unsupported return type
COMPTIME005 Compilation emit failed
COMPTIME006 Method execution failed
COMPTIME007 Serialization failed
COMPTIME011 Array return type not allowed (use IReadOnlyList)
COMPTIME012 Argument must be a constant (no variables allowed)

Limitations

  • Methods must be static
  • The containing class must be partial
  • Return types must be immutable (arrays are not allowed, use IReadOnlyList)
  • Method arguments must be compile-time constant expressions (no variables, only literals and expressions of literals)
  • Methods cannot have side effects that depend on runtime state

License

MIT License

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言