HairColor
可能會必須是一個結構類型,使你想工作的東西。另外,你將不得不做運營商超載。我不確定這是否真的是您想要追求的設計路徑。如果由於某種不正當的原因,你絕對必須這樣做,這是你需要的那種結構定義的(非常粗略,LOL):
public struct HairColorStruct
{
private string m_hairColor;
public void Update()
{
// do whatever you need to do here...
}
// the very basic operator overloads that you would need...
public static implicit operator HairColorStruct(string color)
{
var result = new HairColorStruct();
result.m_hairColor = color;
return result;
}
public static explicit operator string(HairColorStruct hc)
{
return hc.m_hairColor;
}
public override string ToString()
{
return m_hairColor;
}
public static bool operator ==(HairColorStruct from, HairColorStruct to)
{
return from.m_hairColor == to.m_hairColor;
}
public static bool operator ==(HairColorStruct from, string to)
{
return from.m_hairColor == to;
}
public static bool operator !=(HairColorStruct from, HairColorStruct to)
{
return from.m_hairColor != to.m_hairColor;
}
public static bool operator !=(HairColorStruct from, string to)
{
return from.m_hairColor != to;
}
}
然後,您可以重新定義你的Person
對象是這樣的:
public class Person
{
public HairColorStruct HairColor { get; set; }
// whatever else goes here...
}
在你的代碼,HairColor
可以簡單地分配給任何你想要的,只要它是一個string
:
var person = new Person();
person.HairColor = "Blonde";
// this will emit "True" to the console...
if (person.HairColor == "Blonde")
{
Console.WriteLine(true);
}
else
{
Console.WriteLine(false);
}
// update the info per your logic...
person.HairColor.Update();
// you can change the hair color and update again, LOL
person.HairColor = "Brown";
person.HairColor.Update();
您只是通過更新字段來使事情更加複雜。在你的Person類中提供一個更新所有字段的Update方法(使用參數)。 dbms幾乎沒有性能差異。 – 2012-03-28 21:36:40
如果你想更新一列,我會看看我的答案。其他人也是很好的解決方案,但它只是我會做的。 B – Base33 2012-03-28 23:10:08