小编gro*_*amm的帖子

在C#中测试类型安全的枚举模式

我正在使用"类型安全枚举模式"

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

出于测试目的,我需要创建一个无效的Level,我用反射做.

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});
Run Code Online (Sandbox Code Playgroud)

使用反射来访问私有构造函数似乎......对我来说是错误的.是否有更好的方法来制作无效的等级?

c# testing nunit unit-testing design-patterns

1
推荐指数
1
解决办法
1440
查看次数

标签 统计

c# ×1

design-patterns ×1

nunit ×1

testing ×1

unit-testing ×1