我会这样做:
public class Record
{
public int ID {get;set;}
[EditorBrowsable(EditorBrowsableState.Never)]
public int UnknownIntValue {get;set;}
[EditorBrowsable(EditorBrowsableState.Never)]
public bool UnknownIntKnown {get;set;}
[SubSonicIgnore]
public UnknownInt UnknownInt
{
get
{
return new UnknownInt()
{
val = UnknownIntValue,
known = this.UnknownIntKnown
};
}
set
{
this.UnknownIntValue = value.val;
this.UnknownIntKnown = value.known;
}
}
}
public struct UnknownInt
{
public readonly int Val;
public readonly bool Known;
public UnknownInt(int val, bool known)
{
this.Val = val;
this.Known = known;
}
public override string ToString()
{
return String.Format("{0} ({1})",
Val, Known == true ? "known" : "unknown");
}
public override bool Equals(Object obj)
{
return obj is UnknownInt && this == (UnknownInt)obj;
}
public static bool operator ==(UnknownInt x, UnknownInt y)
{
return x.Val == y.Val && x.Known == y.Known;
}
public static bool operator !=(UnknownInt x, UnknownInt y)
{
return !(x == y);
}
}
基本思想是必须拥有存储用户定义类型但对智能感知隐藏的列(System.ComponentModel.EditorBrowsable 属性)。比你有一个隐藏在 SubSonic 的简单存储库中的复杂类型(在这种情况下我更喜欢结构而不是类)。覆盖和运算符重载是可选的,但使使用这种类型更容易。
示例用法:
// 1. Step Create item1
var item1 = new Record();
item1.ID = 1;
item1.UnknownInt = new UnknownInt(1, true);
// 2. Setp Create item2
var item2 = new Record();
item2.ID = 2;
item2.UnknownImt = new UnknownInt(1, false);
if (item1.UnknownInt == item2.UnknownInt)
Console.WriteLine("???");
else
Console.WriteLine("Profit!!!");