Visual Studio の 特殊機能:
『埋め込み言語 (Embedded Language) 』
Section titled “Visual Studio の 特殊機能: 『埋め込み言語 (Embedded Language) 』”やや、隠し機能的なものです。
文字列中の シンタックスハイライト (色付け) 機能
Section titled “ 文字列中の シンタックスハイライト (色付け) 機能”文字列(逐語的文字列リテラルや、生文字列リテラルも含む)は、基本的に1色だけの色付け(Visual Studio の初期設定では、茶色系の色)となりますが、C#のソースジェネレータを記述する際や、JSONデータを書く際、正規表現を書く際に、エディタ内で シンタックスハイライト がないと可読性が低くなります。
そこで Visual Studio の特殊機能として、文字列の前に /* lang=言語名 */ というコメントアウトを記述することで、その言語の シンタックスハイライト がされるようになります。
※ /*lang=言語名*/という風に間の半角スペースを詰めても、/* lang = 言語名 */という風に間に半角スペースを入れても、認識されます。また、大文字・小文字はどちらでも問題ありません。
/* lang=C# */
Section titled “/* lang=C# */”/* lang=C# */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、C# コードのハイライトができます。
これは、ソースジェネレータによって生成されるコードを自前で実装(記述)する際に便利です。
Visual Studio
Section titled “ Visual Studio”/* lang=json */
Section titled “/* lang=json */”/* lang=json */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、明示的に JSON データのハイライトができます。
Visual Studio
Section titled “ Visual Studio”/* lang=xml */
Section titled “/* lang=xml */”/* lang=xml */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、XML マークアップのハイライトができます。
/* lang=regex */
Section titled “/* lang=regex */”/* lang=regex */ または /* lang=regexp */ を付与することで、逐語的文字列リテラルの中で、正規表現のハイライトができます。
Visual Studio
Section titled “ Visual Studio”/* lang=composite */
Section titled “/* lang=composite */”/* lang=composite */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、複合書式指定文字列についてハイライトができます。
Visual Studio
Section titled “ Visual Studio”/* lang=guid */
Section titled “/* lang=guid */”/* lang=guid */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、GUID についてハイライトができます。
Visual Studio
Section titled “ Visual Studio”/* lang=uri */
Section titled “/* lang=uri */”/* lang=uri */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、URI についてハイライトができます。
Visual Studio
Section titled “ Visual Studio”/* lang=route */ ※ASP.NET Core のみ
Section titled “/* lang=route */ ※ASP.NET Core のみ”Microsoft.NET.Sdk.Web ( ASP.NET Core ) プロジェクトにて、/* lang=route */ を付与することで、文字列の中で、ルート(ルーティング)のハイライトができます。
Visual Studio
Section titled “ Visual Studio”/* lang=javascript */
Section titled “/* lang=javascript */”/* lang=javascript */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、JavaScript についてハイライトができます。
/* lang=html */
Section titled “/* lang=html */”/* lang=html */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、HTML マークアップのハイライトができます。
/* lang=css */
Section titled “/* lang=css */”/* lang=css */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、CSS スタイルシートのハイライトができます。
/* lang=sql */
Section titled “/* lang=sql */”/* lang=sql */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で、SQL ステートメントのハイライトができます。
文字列中の シンタックスチェック 機能
Section titled “ 文字列中の シンタックスチェック 機能”文字列(逐語的文字列リテラルや、生文字列リテラルも含む) の前に /* lang=言語名 */ というコメントアウトを記述することで、その言語の シンタックスチェック が行われるようになります。
※ /*lang=言語名*/という風に間の半角スペースを詰めても、/* lang = 言語名 */という風に間に半角スペースを入れても、認識されます。また、大文字・小文字はどちらでも問題ありません。
/* lang=json */
Section titled “/* lang=json */”/* lang=json,~~~ */ を付与した、文字列や生文字リテラル、逐語的文字列リテラルの中の JSON については、以下のシンタックスチェックも行われます。
Visual Studio
Section titled “ Visual Studio”| 記述 | strict (厳密モード) | コメントアウト を許容 | 余分なカンマ を許容 |
|---|---|---|---|
/* lang=json */ | - | ✔ | ✔ |
/* lang=json,AllowComment,TrailingCommas */ | - | ✔ | ✔ |
/* lang=json,strict */ | ✔ | - | - |
/* lang=json,strict,AllowComment */ | ✔ | ✔ | - |
/* lang=json,strict,TrailingCommas */ | ✔ | - | ✔ |
/* lang=json,strict,AllowComment,TrailingCommas */ | ✔ | ✔ | ✔ |
| 記述 | strict (厳密モード) | コメントアウト を許容 | 余分なカンマ を許容 |
|---|---|---|---|
/* lang=json */ | ✔ | - | - |
/* lang=json,strict */
Section titled “/* lang=json,strict */”strict を付記することで、JSON を strict (厳密) モードで構文チェックが行われます。
Visual Studio 例 1. JSONデータの末尾に余分な , (半角カンマ) を記述してしまった時の警告 (strictモード)
Section titled “ Visual Studio 例 1. JSONデータの末尾に余分な , (半角カンマ) を記述してしまった時の警告 (strictモード)”Visual Studio 例 2. JSONデータ内にコメントアウトを記述してしまった時の警告 (strictモード)
Section titled “ Visual Studio 例 2. JSONデータ内にコメントアウトを記述してしまった時の警告 (strictモード)”/* lang=json,strict,AllowComment */
Section titled “/* lang=json,strict,AllowComment */”strict モードの際、AllowComment を付記することで、コメントアウトがあっても、静的解析による構文チェックで、警告 JSON001 が発生しないようになります。
Visual Studio
Section titled “ Visual Studio”/* lang=json,strict,TrailingCommas */
Section titled “/* lang=json,strict,TrailingCommas */”strict モードの際、TrailingCommas を付記することで、余分なカンマがあっても、静的解析による構文チェックで、警告 JSON001 が発生しないようになります。
Visual Studio
Section titled “ Visual Studio”/* lang=json,strict,AllowComment,TrailingCommas */
Section titled “/* lang=json,strict,AllowComment,TrailingCommas */”strict モードの際、AllowComment と TrailingCommas を付記することで、コメントアウトがあっても、余分なカンマがあっても、静的解析による構文チェックで、警告 JSON001 が発生しないようになります。
/* lang=xml */
Section titled “/* lang=xml */”Rider では、/* lang=xml */ を付与した、文字列や生文字リテラル、逐語的文字列リテラルの中の XML については、シンタックスチェックも行われます。
Rider 例 1.
Section titled “ Rider 例 1.”Rider 例 2.
Section titled “ Rider 例 2.”Rider 例 3.
Section titled “ Rider 例 3.”/* lang=javascript */
Section titled “/* lang=javascript */”/* lang=javascript */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中に記述した JavaScript スクリプトの構文チェックが行われます。
/* lang=html */
Section titled “/* lang=html */”/* lang=html */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中に記述した HTML マークアップの構文チェックが行われます。
/* lang=css */
Section titled “/* lang=css */”/* lang=css */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中に記述した CSS スタイルシートの構文チェックが行われます。
/* lang=sql */
Section titled “/* lang=sql */”/* lang=sql */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中に記述した SQL ステートメントの構文チェックが行われます。
文字列中の インテリセンス (入力補完) 機能
Section titled “ 文字列中の インテリセンス (入力補完) 機能”文字列(逐語的文字列リテラルや、生文字列リテラルも含む) の前に /* lang=言語名 */ というコメントアウトを記述することで、その言語での インテリセンス/入力補完 が効くようになります。
※ /*lang=言語名*/という風に間の半角スペースを詰めても、/* lang = 言語名 */という風に間に半角スペースを入れても、認識されます。また、大文字・小文字はどちらでも問題ありません。
/* lang=regex */
Section titled “/* lang=regex */”/* lang=regex */ または /* lang=regexp */ を付与することで、逐語的文字列リテラルの中で、正規表現のインテリセンスが効きます。
Visual Studio
Section titled “ Visual Studio”/* lang=datetime */
Section titled “/* lang=datetime */”/* lang=datetime */ を付与することで、文字列の中で、DateTime のインテリセンスが効きます。
Visual Studio
Section titled “ Visual Studio”/* lang=date */
Section titled “/* lang=date */”/* lang=date */ を付与することで、文字列の中で、DateOnly のインテリセンスが効きます。
Visual Studio
Section titled “ Visual Studio”心の声: ヤーポン法も許すまじですが、アメリカの MM/dd/yyyy はもっと絶許(絶対に許すまじ)です。
### 例 ### - Universal (国際標準) : 2026-02-01 ※大きいほうから書く(年 → 月 → 日) - ja-JP (日本) : 2026/02/01 ※大きいほうから書く(年 → 月 → 日) - en-GB (イギリス) : 01/02/2026 ※小さいほうから書く(日 → 月 → 年) - en-US (アメリカ) : 2/01/2026 ※アメリカだけ変則的(月 → 日 → 年)/* lang=time */
Section titled “/* lang=time */”/* lang=time */ を付与することで、文字列の中で、TimeOnly のインテリセンスが効きます。
Visual Studio
Section titled “ Visual Studio”/* lang=datetimeformat */
Section titled “/* lang=datetimeformat */”/* lang=datetimeformat */ を付与することで、文字列の中で、DateTimeFormat のインテリセンスが効きます。
Visual Studio
Section titled “ Visual Studio”/* lang=javascript */
Section titled “/* lang=javascript */”/* lang=javascript */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で JavaScript スクリプトの入力補完が効きます。
/* lang=html */
Section titled “/* lang=html */”/* lang=html */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で HTML マークアップの入力補完が効きます。
/* lang=css */
Section titled “/* lang=css */”/* lang=css */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で CSS スタイルシートの入力補完が効きます。
/* lang=sql */
Section titled “/* lang=sql */”/* lang=sql */ を付与することで、文字列や生文字リテラル、逐語的文字列リテラルの中で SQL ステートメントの入力補完が効きます。
文字列中の特殊なシンタックス解析(Roslynアナライザー用)機能
Section titled “ 文字列中の特殊なシンタックス解析(Roslynアナライザー用)機能”以下は IDE 上で、シンタックスハイライトや、シンタックスチェックは行われないものの、Roslyn のアナライザーによって、文字列中の特別な解析が行われるものです。
/* lang=composite */
Section titled “/* lang=composite */”複合書式指定文字列 として、解析されます。
/* lang=numeric */
Section titled “/* lang=numeric */”数値書式指定文字列 として、解析されます。
/* lang=enum */
Section titled “/* lang=enum */”列挙型 として、解析されます。
/* lang=timespan */
Section titled “/* lang=timespan */”TimeSpan として、解析されます。
/* lang=StackFrame */
Section titled “/* lang=StackFrame */”スタックトレース として、ツリー状に解析されます。
[StringSyntax] 属性
Section titled “[StringSyntax] 属性”メソッドの仮引数(文字列型)に StringSyntax 属性を付与することで、
実引数の文字列にて、シンタックスハイライトや、シンタックスチェック、インテリセンス等を効かせることができます。
Visual Studio 例 1. C# の、シンタックスハイライト
Section titled “ Visual Studio 例 1. C# の、シンタックスハイライト”Visual Studio 例 2. JSON (厳密モード) の、シンタックスハイライト&シンタックスチェック
Section titled “ Visual Studio 例 2. JSON (厳密モード) の、シンタックスハイライト&シンタックスチェック”Visual Studio 例 3. DateTimeFormat の、インテリセンス
Section titled “ Visual Studio 例 3. DateTimeFormat の、インテリセンス”[RegexPattern] 属性
Section titled “[RegexPattern] 属性”JetBrains IDE 独自機能
... // TODO
[LanguageInjection] 属性
Section titled “[LanguageInjection] 属性”JetBrains IDE 独自機能
... // TODO
一覧(まとめ)
Section titled “一覧(まとめ)”Visual Studio 2026、JetBrains Rider 2026 時点での、一覧です。
Visual Studio (2026 時点)
Section titled “ Visual Studio (2026 時点)”| 言語 | コメントアウト | シンタックス ハイライト | シンタックス チェック | インテリ センス | 内部での 静的解析 |
|---|---|---|---|---|---|
| C# | /* lang=C# */ | ✔ | - | - | ✔ |
| C# | /* lang=C#-test */ | ✔ | - | - | ✔ |
| JSON | /* lang=json */ | ✔ | ✔ | - | ✔ |
| JSON | /* lang=json */,AllowComment,TrailingCommas */ | ✔ | ✔ | - | ✔ |
| JSON (厳密) | /* lang=json,Strict */ | ✔ | ✔ | - | ✔ |
| JSON (厳密) (コメント許容) | /* lang=json,Strict,AllowComment */ | ✔ | ✔ | - | ✔ |
| JSON (厳密) (末尾カンマ許容) | /* lang=json,Strict,TrailingCommas */ | ✔ | ✔ | - | ✔ |
| JSON (厳密) (コメント許容) (末尾カンマ許容) | /* lang=json,Strict,AllowComment,TrailingCommas */ | ✔ | ✔ | - | ✔ |
| 正規表現 | /* lang=regex */ | ✔ | ✔ | ✔ | ✔ |
| 正規表現 | /* lang=regexp */ | ✔ | ✔ | ✔ | ✔ |
| XML | /* lang=xml */ | - | - | - | ✔ |
| 複合書式 指定文字列 | /* lang=composite */ | ✔ | - | - | ✔ |
| 数値書式 指定文字列 | /* lang=numeric */ | - | - | - | ✔ |
| 列挙型 | /* lang=enum */ | - | - | - | ✔ |
| 日付時刻 (DateTime) | /* lang=datetime */ | - | - | ✔ | ✔ |
| 日付 (DateOnly) | /* lang=date */ | - | - | ✔ | ✔ |
| 時刻 (TimeOnly) | /* lang=time */ | - | - | ✔ | ✔ |
| 時間間隔 (TimeSpan) | /* lang=timespan */ | - | - | - | ✔ |
| 日付時刻 指定形式 (DateTime Format) | /* lang=DateTimeFormat */ | - | - | ✔ | ✔ |
| GUID | /* lang=guid */ | ✔ | - | - | ✔ |
| URI | /* lang=uri */ | ✔ | - | - | ✔ |
| Web ルーティング | /* lang=route */ | ✔ | - | - | ✔ |
| スタック トレース | /* lang=StackFrame */ | - | - | - | ✔ |
JetBrains Rider (2026 時点)
Section titled “ JetBrains Rider (2026 時点)”| 言語 | コメントアウト | シンタックス ハイライト | シンタックス チェック | 入力補完 |
|---|---|---|---|---|
| C# | /* lang=C# */ | ✔ | ✔ | - |
| JSON | /* lang=json */ | ✔ | ✔ | - |
| 正規表現 | /* lang=regex */ | ✔ | ✔ | ✔ |
| 正規表現 | /* lang=regexp */ | ✔ | ✔ | ✔ |
| ECMAScript 準拠 正規表現 | /* lang=jsregexp */ | ✔ | ✔ | ✔ |
| XML | /* lang=xml */ | ✔ | ✔ | - |
| HTML | /* lang=html */ | ✔ | ✔ | ✔ |
| CSS | /* lang=css */ | ✔ | ✔ | ✔ |
| JavaScript | /* lang=javascript */ | ✔ | ✔ | ✔ |
| SQL | /* lang=sql */ | ✔ | ✔ | ✔ |
| VBA | /* lang=vba */ | ✔ | - | - |
| F# | /* lang=F# */ | ✔ | - | - |
| Java | /* lang=java */ | ✔ | - | - |
| C | /* lang=C */ | ✔ | - | - |
| C++ | /* lang=C++ */ | ✔ | - | - |
| shell | /* lang=sh */ | ✔ | - | ✔ |
| bash | /* lang=bash */ | ✔ | - | ✔ |
| YAML | /* lang=yaml */ | ✔ | - | - |




























