/*
.Net/C# 實現真正的唯讀屬性 (ReadOnly Property)
當類的私人成員是簡單類型時,只需為該成員提供 public { get; } 的訪問器即可實現唯讀屬性。
當類的私人成員不是簡單類型(如: ArrayList、Hashtable 等)時,
如果僅為該成員提供 public { get; } 的訪問器而實現唯讀屬性是遠遠不夠的!
因為該屬性 ArrayList、Hashtable 還可以被執行 Add(..)、Clear()、Remove(...) 等方法!
經 【身披七彩祥雲 腳踏金甲聖衣】的 "思歸 Saucer" 點撥,
參閱 Reflector: ArrayList.ReadOnly(...) static Method
搞定 ReadOnlyHashtable !
*/
//using System;
//using System.Collections;
//using System.Runtime.Serialization;
namespace Microshaoft
{
public class WithReadOnlyPropertyClass
{
public WithReadOnlyPropertyClass()
{
this._Hashtable = new System.Collections.Hashtable();
this._Hashtable.Add("1", "aaa");
this._Hashtable.Add("2", "bbb");
this._Hashtable.Add("3", "ccc");
this._ArrayList = new System.Collections.ArrayList();
this._ArrayList.Add("1");
this._ArrayList.Add("2");
this._ArrayList.Add("3");
}
private System.Collections.ArrayList _ArrayList;
public System.Collections.ArrayList ReadOnlyArrayList
{
get
{
//.Net Framework 已經實現
return System.Collections.ArrayList.ReadOnly(this._ArrayList);
}
}
private System.Collections.Hashtable _Hashtable;
public System.Collections.Hashtable ReadOnlyHashTable
{
get
{
return ReadOnlyHashtable.ReadOnly(this._Hashtable);
}
}
//.Net Framework 懶得實現 ReadOnlyHashtable ???
private class ReadOnlyHashtable : System.Collections.Hashtable
{
//《Refactoring: Improving the Design of Existing Code》
// 3.21 Refused Bequest: Replace Inheritance with Delegation
//如果不想修改superclass,還可以運用 Replace Inheritance with Delegation 來達到目的。
//也就是以委託取代繼承,在 subclass 中建立一個 Field 來儲存 superclass 對象,
//去除 subclass 對 superclass 的繼承關係,委託或調用 superclass 的方法來完成目的。
//這裡的委託不是 .Net 的 Delegation !
//僅僅就是代理人、替代品、代表的意思!
private System.Collections.Hashtable _Hashtable;
private ReadOnlyHashtable(System.Collections.Hashtable Hashtable)
{
this._Hashtable = Hashtable;
}
public static System.Collections.Hashtable ReadOnly(System.Collections.Hashtable Hashtable)
{
if (Hashtable == null)
{
throw new System.ArgumentNullException("Hashtable");
}
//多態
return new ReadOnlyHashtable(Hashtable);
}
private string _s = "集合是唯讀。";
//重寫 override 所有 "寫" 操作的方法,執行階段錯誤,如調用該方法則拋出異常
public override void Add(object key, object value)
{
throw new System.NotSupportedException(this._s);
}
public override void Clear()
{
throw new System.NotSupportedException(this._s);
}
public override object Clone()
{
ReadOnlyHashtable roht = new ReadOnlyHashtable(this._Hashtable);
roht._Hashtable = (System.Collections.Hashtable) this._Hashtable.Clone();
return roht;
}
//重寫 override 方法
public override bool Contains(object key)
{
//用代理的 Hashtable Field 對象的執行個體方法重寫
//return base.Contains(key);
return this._Hashtable.Contains(key);
}
public override bool ContainsKey(object key)
{
return this._Hashtable.ContainsKey(key);
}
public override bool ContainsValue(object value)
{
return this._Hashtable.ContainsValue(value);
}
public override void CopyTo(System.Array array, int arrayIndex)
{
this._Hashtable.CopyTo(array, arrayIndex);
}
public override System.Collections.IDictionaryEnumerator GetEnumerator()
{
//經重寫改為調用代理成員對象的同名執行個體方法
return this._Hashtable.GetEnumerator();
}
// protected override int GetHash(object key)
// {
// //protected 成員不能通過代理對象的執行個體方法重寫
// return base.GetHash(key);
// }
//
// protected override bool KeyEquals(object item, object key)
// {
// return base.KeyEquals(item, key);
// }
public override void Remove(object key)
{
throw new System.NotSupportedException(this._s);
}
public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
{
this._Hashtable.GetObjectData(info, context);
}
public override void OnDeserialization(object sender)
{
this._Hashtable.OnDeserialization(sender);
}
public override bool IsReadOnly
{
get
{
return this._Hashtable.IsReadOnly;
}
}
public override bool IsFixedSize
{
get
{
return this._Hashtable.IsFixedSize;
}
}
public override bool IsSynchronized
{
get
{
return this._Hashtable.IsSynchronized;
}
}
public override System.Collections.ICollection Keys
{
get
{
return this._Hashtable.Keys;
}
}
public override System.Collections.ICollection Values
{
get
{
return this._Hashtable.Values;
}
}
public override object SyncRoot
{
get
{
return this._Hashtable.SyncRoot;
}
}
public override int Count
{
get
{
return this._Hashtable.Count;
}
}
//索引器別忘了重寫 override
public override object this[object key]
{
get
{
//使用 代理對象
return this._Hashtable[key];
}
set
{
throw new System.NotSupportedException(this._s);
}
}
}
}
}
class AppTest
{
static void Main(string[] args)
{
Microshaoft.WithReadOnlyPropertyClass x = new Microshaoft.WithReadOnlyPropertyClass();
System.Console.WriteLine("ReadOnlyArrayList Property Test:");
//多態
System.Collections.ArrayList al = x.ReadOnlyArrayList;
foreach (object o in al)
{
System.Console.WriteLine("Value: {0}", o);
}
System.Console.WriteLine();
System.Collections.IEnumerator ie = al.GetEnumerator();
while (ie.MoveNext())
{
System.Console.WriteLine("Value: {0}", ie.Current);
}
System.Console.WriteLine("按/"y/"健,執行下面寫操作將拋出異常,按其他健跳過寫操作繼續:");
if (System.Console.ReadLine().ToLower() == "y")
{
System.Console.WriteLine("拋出異常...");
al.Clear();
}
System.Console.WriteLine("/nReadOnlyHashTable Property Test:");
//多態
System.Collections.Hashtable ht = x.ReadOnlyHashTable;
foreach (System.Collections.DictionaryEntry e in ht)
{
System.Console.WriteLine("Key: {0} , Value: {1}", e.Key, e.Value);
}
System.Console.WriteLine();
System.Collections.IDictionaryEnumerator ide = ht.GetEnumerator();
while (ide.MoveNext())
{
System.Console.WriteLine("Key: {0} , Value: {1}", ide.Key, ide.Value);
}
System.Console.WriteLine("按/"y/"健,執行下面寫操作將拋出異常,按其他健跳過寫操作繼續:");
if (System.Console.ReadLine().ToLower() == "y")
{
System.Console.WriteLine("拋出異常...");
ht.Clear();
}
System.Console.ReadLine();
}
}