我有一个自定义对象,从旧数据库布尔值映射到C#布尔(并再次)。处理与自定义布尔类型的比较?
我的自定义布尔对象是这样的:
public class S2kBool : IUserDefinedType {
public bool Value { get; set; }
public Type SupportedType { get { return typeof(string); } }
// These are the values used to represent booleans in the database
public const string TrueValue = "Y";
public const string FalseValue = "N";
public static S2kBool True {
get { return new S2kBool(true); }
}
public static S2kBool False {
get { return new S2kBool(false); }
}
public S2kBool() : this(false) { }
public S2kBool(bool value) {
this.Value = value;
}
// Called when a property of this type is populated from the database
public void FromSimpleDataType(object value) {
this.Value = value.ToString() == TrueValue;
}
// Called when a property of this type is inserted into the database
public object ToSimpleDataType() {
return this.Value ? TrueValue : FalseValue;
}
}
我希望能够做这样的事情:
public class TestObject {
public S2kBool IsActive = S2kBool.True;
}
TestObject tObj = new TestObject();
if (tObj.IsActive == S2kBool.True) {
// the above would evaluate to true
}
我见过之间做比较几种不同的方法对象,但我不确定要使用哪一个。
编辑:更重要的是,将有可能像做以下,并已C#比较期间处理S2kBool对象作为一个实际的布尔?它也应该允许与其他S2kBool对象进行比较。
if (tObj.IsActive == true) { ... }
谢谢!虽然,你能解释一下为什么我想让它成为一个不可变的结构吗? – 2009-02-24 21:58:18