Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
SourceGeneratorのマーカー属性問題について
Search
tkym
February 28, 2026
Programming
250
0
Share
SourceGeneratorのマーカー属性問題について
tkym
February 28, 2026
More Decks by tkym
See All by tkym
今さら聞けないCancellationToken
htkym
0
220
GitHub Copilot CLIのいいところ
htkym
2
1.3k
GitHubCopilotCLIをはじめよう.pdf
htkym
0
360
RoslynAnalyzerをつかってみよう
htkym
0
790
SourceGeneratorのススメ
htkym
0
900
Entity Framework Core におけるIN句クエリ最適化について
htkym
0
220
.NET 10のEntity Framework Coreの新機能
htkym
0
1.1k
.NET 10のBlazorの期待の新機能
htkym
0
1.3k
Azure Static Web Appsでユニバーサルリンク
htkym
0
55
Other Decks in Programming
See All in Programming
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.3k
さぁV100、メモリをお食べ・・・
nilpe
0
130
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.1k
ふつうのFeature Flag実践入門
irof
7
3.6k
プラグインで拡張される Context をtype-safe にする難しさと設計判断
kazupon
2
590
Spec-Driven Development with AI-Agents: From High-Level Requirements to Working Software
antonarhipov
2
450
These Five Tricks Can Make Your Apps Greener, Cheaper, & Nicer
hollycummins
0
270
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
440
ADKを使って簡単にAIエージェントを作ってみよう
k1mu21
0
220
Claspは野良GASの夢をみるか
takter00
0
160
The NotImplementedError Problem in Ruby
koic
1
590
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
450
Featured
See All Featured
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
2.8k
Making Projects Easy
brettharned
120
6.7k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
830
How to Think Like a Performance Engineer
csswizardry
28
2.6k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
Measuring Dark Social's Impact On Conversion and Attribution
stephenakadiri
2
210
It's Worth the Effort
3n
188
29k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Navigating Team Friction
lara
192
16k
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
310
Faster Mobile Websites
deanohume
310
31k
Transcript
Source Generatorの マーカー属性問題について 2026/02/28 .NETラボ勉強会2月
高山博司 ・ FutureOne株式会社 ・ C#/.NET/Blazor tkym @h_tkymx
Source Generator
C# Source Generator コンパイル時のメタプログラミングツール ・ 新しいC#ソースファイルを生成し,自動的に追加 ・ 開発中に実行 ・ 実行時のパフォーマンスが向上
・ リフレクションを排除 ・ AOTコンパイルとツリーシェイキングを可能に 前回の内容 https://zenn.dev/poipoionigiri/articles/57f1c33a7f5baa
どのクラス、メソッド、あるいはプロパティに対してコードを生成すべきか Syntactic Filtering: 非常に高速。テキスト情報やトークンの種類だけで判断する。 Semantic Analysis: 非常に高コスト。型解決、継承関係の確認、他アセンブリのメタデータ参照を含む。
マーカー属性 ForAttributeWithMetadataName
マーカー属性 AllInterfaces はやめましょう... ・ 推移的な依存関係 ・ アセンブリ境界の越境 ・ 増分ビルドの破壊
Blazor Form Validation - .NET 10
Marker Attribute Problem
マーカー属性の定義場所 どこにマーカー属性を定義すればよい? マーカー属性もSource Generatorでつくる?
CS0246(型または名前空間が見つかりません) × Attributeを出すだけのGeneratorを作る × 出力コードの中にAttributeの定義も混ぜ込む
Attribute用のライブラリを作る △ 2つも依存関係もつの・・・? スタンドアロンなSource Generatorがいい !
RegisterPostInitializationOutput ◎ ジェネレーターの起動時にフックして,固定のコードを差し込める. 「マーカー属性を追加してね」と言わんばかりのメソッド
CS0436 : 型の競合 複数プロジェクトにSource Generatorを入れていて, それらが依存関係にある場合に, [CS0436 : 型の競合 ]
警告が出る. → internal class
internal...
CS0436 : 型の競合(InternalsVisibleTo編) [InternalsVisibleTo]: 指定されたアセンブリに対して、通常は internal アクセスレ ベルで保護されている型やメンバーへのアクセス権を付与する宣言的な属性
CS0436 : 型の競合 (InternalsVisibleTo編) × Source Generatorに属性クラスを直接含め、DLL 自体を通常の参照としても使う ・ Source
Generatorの依存がユーザーに流れてしまう. ・ バージョンの競合が起こる可能性 ・ analyzerとして使ってほしい
CS0436 : 型の競合 (InternalsVisibleTo編) Source Generator パッケージにAttribute DLLをパックする
CS0436 : 型の競合 (InternalsVisibleTo編) .NET 10 ・ 出力するアトリビュートに[Embedded]を付与 ・ ResisterPostInitializeationOutput時に
AddEmdeddedAttributeDefinitionを呼ぶ
まとめ Source Generator の Marker Attribute Problem ・ RegisterPostInitializationOutputで Source
Generatorで生成することを考える. → Attribute は internal class ・ 複数プロジェクトで使いたい場合は,依存関係に注意 → .NET 10 では,[Embedded]を使う → それ以前では,一工夫した方が使いやすいかも