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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
tkym
February 28, 2026
Programming
0
110
SourceGeneratorのマーカー属性問題について
tkym
February 28, 2026
Tweet
Share
More Decks by tkym
See All by tkym
SourceGeneratorのススメ
htkym
0
740
Entity Framework Core におけるIN句クエリ最適化について
htkym
0
180
.NET 10のEntity Framework Coreの新機能
htkym
0
970
.NET 10のBlazorの期待の新機能
htkym
0
1.2k
Azure Static Web Appsでユニバーサルリンク
htkym
0
37
Aspireとazdでデプロイが簡単に
htkym
0
34
Azure Blob Storageでユニバーサルリンク
htkym
0
210
Other Decks in Programming
See All in Programming
CSC307 Lecture 10
javiergs
PRO
1
690
Go 1.26でのsliceのメモリアロケーション最適化 / Go 1.26 リリースパーティ #go126party
mazrean
1
320
コーディングルールの鮮度を保ちたい / keep-fresh-go-internal-conventions
handlename
0
120
AI主導でFastAPIのWebサービスを作るときに 人間が構造化すべき境界線
okajun35
0
440
atmaCup #23でAIコーディングを活用した話
ml_bear
4
720
RAGでハマりがちな"Excelの罠"を、データの構造化で突破する
harumiweb
8
2.2k
社内規程RAGの精度を73.3% → 100%に改善した話
oharu121
12
7k
grapheme_strrev関数が採択されました(あと雑感)
youkidearitai
PRO
1
190
PJのドキュメントを全部Git管理にしたら、一番喜んだのはAIだった
nanaism
0
230
New in Go 1.26 Implementing go fix in product development
sunecosuri
0
120
CSC307 Lecture 12
javiergs
PRO
0
450
モジュラモノリスにおける境界をGoのinternalパッケージで守る
magavel
0
3.4k
Featured
See All Featured
What does AI have to do with Human Rights?
axbom
PRO
1
2k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.2k
A Soul's Torment
seathinner
5
2.4k
Building an army of robots
kneath
306
46k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
200
Designing for humans not robots
tammielis
254
26k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
400
The Pragmatic Product Professional
lauravandoore
37
7.2k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Facilitating Awesome Meetings
lara
57
6.8k
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]を使う → それ以前では,一工夫した方が使いやすいかも