LoginSignup
5
5

More than 5 years have passed since last update.

C#で「あるコードがコンパイルエラーになる」ことを確かめるコード。

Last updated at Posted at 2015-12-16

方針

Microsoft.CodeAnalysis.CSharp.Scriptingを使う。
エラーになるようなスクリプトをCompile()して、エラーになることを確かめる。

こんな感じ

var options = ScriptOptions.Default
    .AddReferences(/* */)
    .WithImports(/* */);
var script = CSharpScript.Create(
    /* エラーになることを期待するコード */
    ,options);

var result = script.Compile();
foreach (var x in result)
    Console.WriteLine(x); //エラーメッセージ

何に使うの?

APIでこういう書き方はエラーにするってのをテストしたい時とか?

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5