看板 C_Sharp 關於我們 聯絡資訊
因為數據更新起來的計算比較複雜 為了避免在大量寫入時造成效能變差 因此設計成更新、寫入數值的動作分開 但這樣一來,如果欄位一多,就會出現一堆類似的程式碼 請問有辦法讓重複的工作減少嗎? 還是只能一個一個欄位去寫get set? =================================================================== public class A { private bool isChange = false; public bool IsChange { get { return this.isChange; } } private string name = "未命名"; public string Name { get { if (this.isChange) this.Reflash(); return this.name; } set { this.isChange = true; this.name = value; } } private int number = 0; public int Number { get { if (this.isChange) this.Reflash(); return this.number; } set { this.isChange = true; this.number = value; } } public void Reflash() { this.isChange = false; //do something reflash } } ============================================================== 第二個問題 如果要拷貝一個class 請問該怎麼做? 例如有個Class A A a = new a(); A b b = a; //錯誤! 複製的是參照! 我是想在Class A中加入一個GetCopy方法,然後取得複製 public A GetCopy() { A b = new b() b.name = this.name; b.number = this.number; (略) return b; } 欄位少的話還好,但欄位一多的話...... 就很麻煩了...... 萬一有二十幾個欄位,就得一個一個慢慢打 而且一漏掉就會出問題 -- 20330 6/17 - □ (本文已被吃掉) 幹!這梗有毒...救命~~ 20331 6/17 - □ (本文已被吃掉) 〒 〒 20332 1 6/17 - □ (本文已被吃掉) ▼▼▼▼ 20333 XX 6/17 - 囧 (哈哈拎北有毒) \▲▲▲▲\ = ●20334 1 6/17 - □ (本文已被吃掉) 20335 6/17 - □ (本文已被吃掉) 口卡口卡嘗百草 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.27.109.99
kinwind:可以使用reflection http://tinyurl.com/yevjgw8 08/15 23:09
SDNiceBoat:看不太懂,不過還是謝謝1F 08/15 23:40
kinwind:或者可以找Deep copy的程式 http://tinyurl.com/awyoy8 08/15 23:45
deuter:clone可以先serialize後 再deserialize成物件 08/15 23:51
deuter:http://tinyurl.com/26vfm4a 08/15 23:52
deuter:第一個問題通常可以用 entity framework 08/16 00:04
deuter:如果不能使用的話 也可以自己寫一個簡單的tool來產生程式碼 08/16 00:04
SDNiceBoat:謝謝,正在努力瞭解中 08/16 08:26