Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
C

comptime

> 编程语言
Open source

Comptime brings meta-programming capabilities to C#, enabling compile-time code generation and evaluation.

573 stars0 likes0 views
WebsiteGitHub

About

Comptime brings meta-programming capabilities to C#, enabling compile-time code generation and evaluation.

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 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

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