.Net/C# 实现真正的只读的 Hashtable 类型的属性 (ReadOnly Hashtable Property)
/*
.Net/C# 实现真正的只读属性 (ReadOnly Property)
当类的私有成员是简单类型时,只需为该成员提供 public { get; } 的访问器即可实现只读属性。
当类的私有成员不是简单类型(如: ArrayList、Hashtable 等)时,
如果仅为该成员提供 public { get; } 的访问器而实现只读属性是远远不够的!
因为该属性 ArrayList、Hashtable 还可以被执行 Add(..)、Clear()、Remove(...) 等方法!
经 【身披七彩祥云 脚踏金甲圣衣】的 "思归 Saucer" 点拨,
参阅 Reflector: ArrayList.ReadOnly(...) static Method
搞定 ReadOnlyHashtable !
但是 实现 ReadOnly DataTable DataRow 等还是更有难度的!
*/
//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();
}
}
转载于:https://www.cnblogs.com/Microshaoft/archive/2005/03/28/127527.html
.Net/C# 实现真正的只读的 Hashtable 类型的属性 (ReadOnly Hashtable Property)相关推荐
- JPQL设置自增长、只读、文本类型等的注解
JAVA中使用JPQL 一种设置id自动生成,自增长的方法 private long id;@Id @GeneratedValue(generator="_native") @Ge ...
- java hashtable 并发_Java 并发容器 —— Hashtable 与 Collections.synchronizedMap(HashMap) 的区别...
Hashtable 部分源码 以 Hashtable 的 put 方法为例: Hashtable 保证线程安全的方式在 方法前加上 synchronized 关键字(锁的是类的实例) Collecti ...
- ClickHouse【环境搭建 02】设置用户密码的两种方式(明文+SHA256)及新用户添加及只读模式 Cannot execute query in readonly mode 问题解决
1.查看user.xml文件可知设置密码的多种方式 <!-- Password could be specified in plaintext or in SHA256 (in hex form ...
- Hashtable、HashMap 与 HashTable区别、HashMap、Hashtable和TreeMap、 LinkedHashMap
目录 Hashtable的函数都是同步的 HashMap 与 HashTable区别 HashMap.Hashtable和TreeMap LinkedHashMap 特殊新增的构造器 TreeMap ...
- c# ArrayList 和 Hashtable 的使用
ArrayList // 从1个集合中拿出某个元素,用完了之后将元素返还给该集合 public ArrayList EnemyEvent = new ArrayList();// 不同于 Java中 ...
- DataTable转换成IList
本文转载自http://blog.csdn.net/chentaihan/article/details/6407284 作者:陈太汉 在用C#作开发的时候经常要把DataTable转换成IL ...
- 【翻译】SILVERLIGHT设计时扩展(注:内容超长,请用IE浏览)
原文链接:Silverlight Design Time Extensibility --By Justin Angel (Microsoft Silverlight Toolkit Program ...
- 优化反射性能的总结(上)
原文链接:http://www.cnblogs.com/fish-li/archive/2013/02/18/2916253.html 优化反射性能的总结(上) 阅读目录 开始 用Emit方法优化反射 ...
- 第99篇 C++数据结构(九)散列表
第99篇 C++数据结构(九)散列表 1.散列表简介 1.1.散列函数 1.2.散列冲突解决方案 2.数据节点 3.实现 3.1.变量 3.2.方法 4.测试 4.1.测试代码 4.2.输出结果 5. ...
最新文章
- mysql 实现yyyyww_java – LocalDate无法使用’yyyy’解析’ww’
- Ubuntu 对比 CentOS 后该如何选择?
- 华为2015年实习生招聘考试试题
- 利用bootstrap插件设置时间
- executor线程池框架_如何使用Java 5 Executor框架创建线程池
- MongoDB数据分布不均的解决方案
- 深度干货|云原生分布式数据库 PolarDB-X 的技术演进
- botstrap-栅格布局与栅格偏移
- Kinect for Windows SDK开发初体验(二)操作Camera
- Python:PDF文件转图像
- 微博表情包大全,截止2022年5月
- 最常用的四种大数据分析方法
- 论文阅读:Which Has Better Visual Quality: The Clear BlueSky or a Blurry Animal?
- 电脑 蓝屏报错:SYSTMEM SCAN AT RAISED IRQL CAUGHT IMPROPER DRIVER UNLOAD
- iPad能装Android系统,我错了,原来iPad真的能装Windows和MacOS系统
- vi编辑器中的常用命令
- 二手车 电商+互联网金融的三种新玩法
- PMS-adb install安装应用流程(Android L)
- 金仓数据库KingbaseES GIN 索引
- 旧电脑也能安装 Windows 11?微软:可以,但后果自负