在下面的代碼中,我想使用靜態加載方法中的代碼來刷新對象......但是如何用新對象重置當前對象?複製字段是唯一的方法嗎?用新的對象重置它
class WIP
{
// <Snipped> Various other properties...
public Boolean Refresh()
{
// Need to change the current object with the updated object
this = WIP.Load(this.ObjectID); // Says this is readonly...
return true;
}
public static WIP Load(long ObjectID)
{
// This static method fetches the data from DB and returns the object.
}
}
編輯:我剛剛張貼的問題後,這個想法......有沒有在這個任何陷阱?
class WIP
{
// <Snipped> Various other properties...
public Boolean Refresh()
{
// This method fetches the data from DB and updates the object.
}
public static WIP Load(long ObjectID)
{
WIP newObject = new WIP();
newObject.ObjectID = ObjectID;
newObject.Refresh();
return newObject;
}
}
不,你不能那樣做。 – leppie 2010-11-09 08:01:17
這會令人困惑,因爲對象的「加載」會將負載委託給對象的刷新方法。 – 2010-11-09 08:13:51