转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
.Net/C# 实现真正的只读属性 (ReadOnly Property)         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1497 更新时间:2009/4/23 10:43:23

/*

.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

[1] [2]  下一页


[Web开发]关于Ehlib的ReadOnly小BUG修正。  [Web开发]readonly vs. const [C#]
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台