.Net/C# 实现真正的只读属性 (ReadOnly Property)

/*

.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();
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值