「typesafe列挙型パターン」を利用しています
public class Level
{
public static readonly Level Low = new Level(0, "Low");
public static readonly Level Medium = new Level(1, "Medium");
public static readonly Level High = new Level(2, "High");
private int _value;
private string name;
private Level(int value, string name)
{
_value=value;
_name=name;
}
}
テスト目的で、リフレクションを使用して無効なレベルを作成する必要があります。
int id = -1;
string value = "invalid";
var constructor = typeof(Level).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] {typeof(int), typeof(string)}, null);
var invalidLevel = (Level)constructor.Invoke(new object[] {id, value});
リフレクションを使用してプライベートコンストラクターにアクセスするのは....私には間違っているようです。無効なレベルを作成するより良い方法はありますか?