好吧,我一直试图找到有关这方面的任何信息.我构建了一个小类来查看为字符串实现类型安全枚举的难度,因为我想将它们用于数据库字段名等.我从来不喜欢枚举只能用于整数的事实.
但是,即使我已经implicit operator为这个类实现了一个,每次我尝试使用它时,它都会给我一个无效的强制转换异常.我很茫然,因为我觉得我的代码在这一点上没有任何问题.
这是班级:
/// <summary>
/// SBool - type-safe-enum for boolean strings
/// </summary>
public sealed class SBool
{
private readonly String name;
private readonly int value;
// these guys were for conversions. They might work better in another case,
// but for this one, they weren't very helpful.
// ((I.e. they didn't work either.))
//private static readonly Dictionary<SBool, String> stringsByBool = new Dictionary<SBool, String>();
//private static readonly Dictionary<String, SBool> boolsByString = new Dictionary<String, SBool>();
public static …Run Code Online (Sandbox Code Playgroud)