代码如下:

1.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConfigurationLibrary
{
public class ConfigElement
{
public int Id { get; set; }
public string Value { get; set; }
}
}

2.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConfigurationLibrary
{
public class ConfigurationContainer
{
private ReadOnlyDictionary<string, ConfigElement> _configuration;
private Dictionary<string, ConfigElement> _mutableConfiguration; public ReadOnlyDictionary<string, ConfigElement> Configuration
{
get
{
_configuration =
new ReadOnlyDictionary<string, ConfigElement>(_mutableConfiguration);
return _configuration;
}
} public ConfigurationContainer()
{ _mutableConfiguration = new Dictionary<string, ConfigElement>();
_mutableConfiguration.Add("key", new ConfigElement { Id=, Value="value1"});
_mutableConfiguration.Add("key1", new ConfigElement { Id = , Value = "value1" });
_mutableConfiguration["key2"] = new ConfigElement { Id = , Value = "value1" };
} public bool AddToConfiguration(string key, ConfigElement value)
{
if (ConfigurationAllowed(key, value))
{
_mutableConfiguration.Add(key, value);
return true;
}
return false;
} private bool ConfigurationAllowed(string key, ConfigElement value)
{
// Put in your checks and balances
// here and return the appropriate result
return true;
}
}
}

3.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfigurationLibrary; namespace ConfigurationUser
{
public class ConfigurationConsumer
{
IReadOnlyDictionary<string, ConfigurationLibrary.ConfigElement> _config; public ConfigurationConsumer()
{
_config = new ConfigurationLibrary
.ConfigurationContainer().Configuration;
} public void DoSomething()
{
if (_config.ContainsKey("key"))
{
// Do Something
Console
.WriteLine(string.Format("Did Something, Got - {0}",
_config["key"].Value));
}
else
{
// Do Something Else.
Console.WriteLine("Did Something Else");
}
} public void BeNaughtyWithConfiguration()
{
IDictionary<string, ConfigElement> convertToReadWrite
= (IDictionary<string, ConfigElement>)_config;
ConfigElement element = convertToReadWrite["key"];
element.Value = "Haa Haa";
Console.WriteLine(element.Value);
Console.WriteLine(convertToReadWrite["key"].Value);
Console.ReadLine(); // 上面的代码都能运行通过,下面这行代码将抛出异常。
convertToReadWrite.Add("Key12345", new ConfigElement { Id = , Value = "Haa Haa" });
}
}
}

4.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ReadOnlyCollectionSample
{
class Program
{
static void Main(string[] args)
{
ConfigurationUser.ConfigurationConsumer consumer =
new ConfigurationUser.ConfigurationConsumer();
consumer.DoSomething();
Console.ReadLine();
consumer.BeNaughtyWithConfiguration();
}
}
}

谢谢浏览!

代码演示 .NET 4.5 自带的 ReadonlyCollection 的使用的更多相关文章

  1. 代码详解:TensorFlow Core带你探索深度神经网络“黑匣子”

    来源商业新知网,原标题:代码详解:TensorFlow Core带你探索深度神经网络“黑匣子” 想学TensorFlow?先从低阶API开始吧~某种程度而言,它能够帮助我们更好地理解Tensorflo ...

  2. 14种网页jQuery和css3特效插件代码演示

    1.网页table增删样式代码 演示和下载地址 2.jQuery左右滑动幻灯片插件 演示和下载地址 3.jQuery文字轮播焦点图 演示和下载地址 4.网页文字焦点图切换 演示和下载地址 5.jQue ...

  3. 9种jQuery和css3图片动画特效代码演示

    1.自由旋转的jQuery图片 演示和下载地址 2.css3阴影动画效果 演示和下载地址 3.拉窗帘特效图片 演示和下载地址 4.css3文字特效动画 演示和下载地址 5.css3时钟代码 演示和下载 ...

  4. java 覆盖hashCode()深入探讨 代码演示样例

    java 翻盖hashCode()深入探讨 代码演示样例 package org.rui.collection2.hashcode; /** * 覆盖hashcode * 设计HashCode时最重要 ...

  5. javascript 压缩空格代码演示

          压缩空格代码演示 主要是讲解 压缩一个字符串两段空格          例如:javascript函数里的空格不论是这样     var s = "Hello World     ...

  6. 单元测试_JUnit常用单元测试注解介绍及代码演示

    JUnit常用单元测试注解介绍及代码演示   by:授客 QQ:1033553122 1. 测试环境 1 2. 基础概念 1 3. 常用Annotation 1 4. 运行环境配置 3 maven配置 ...

  7. Java用代码演示String类中的以下方法的用法

    用代码演示String类中的以下方法的用法 (1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true (2)char charAt(int index): 返回索引上 ...

  8. FFmpeg(9)-解码器解码代码演示(FFmpeg调用MediaCodec实现硬解码、多线程解码、及音视频解码性能测试)

    一.AVFrame 用来存放解码后的数据. [相关函数] AVFrame *frame = av_frame_alloc();                       // 空间分配,分配一个空间 ...

  9. 代码演示神器——jsfiddle

    目录: 1. 介绍 2. jsfiddle的具体使用 3. 总结 1. 介绍 很多时候,我们需要在我们写的文章或博客中,即时显示出我们写的demo,能方便的解释出我们的思路.很久之前我也写过一篇文章, ...

随机推荐

  1. paip. dsl 编程语言优点以及 常见的dsl

    paip. dsl 编程语言优点以及  常见的dsl 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn. ...

  2. chrome浏览器扩展的事件处理

    关于chrome扩展开发的栗子已经有很多了,问问度娘基本能满足你的欲望, 我想说的是扩展和页面间的数据传递问题. 我们知道写扩展有个必须的文件就是“manifest.json”, 这个里面定义了一个和 ...

  3. mac下mysql数据库的配置

    这里记录一下. 之前在mac下使用brew install mysql安装,但是安装完成后发现密码不好修改,上网搜了下发现mac下使用命令行安装mysql确实存在很多问题,这一点确实远不如Ubuntu ...

  4. windows远程连接Linux(Ubuntu)的方法

    需要做的工作: 1.在Linux(Ubuntu)端安装.设置好SSH 2.下载putty,并通过putty的SSH连接登录Linux 一 .如何在Linux(Ubuntu)端安装.设置好SSH,获取I ...

  5. FIREDAC操作SQLITE内存数据库

    SQLite不仅可以把数据库放在硬盘上,还可以放在内存中,经测试,同样条件下数据库放在内存中比放在硬盘上插入记录速度快差不多3倍. 但数据库放在内存中时有如下缺陷: 1.断电或程序崩溃后数据库就会消失 ...

  6. android: 使用 IntentService

    9.5.2 使用 IntentService 话说回来,在本章一开始的时候我们就已经知道,服务中的代码都是默认运行在主线程 当中的,如果直接在服务里去处理一些耗时的逻辑,就很容易出现 ANR(Appl ...

  7. MySQL实现定时任务

    如果要每30秒执行以下语句 '; 可以给MySQL建个定时任务,具体如下: delimiter // /* 设定语句终结符为 //,因存储过程语句用;结束 */ 一.查看event是否开启 show ...

  8. CCNA实验4:HDLC和PPP

    一.HDLC封装 router9和11上分别配置s0/0如下 conf t int s0/0 encapsulation hdlc do show int s0/0 ip address x.x.x. ...

  9. 调试X Server

    发现错误 运行X之后出现了错误: Program received signal SIGSEGV, Segmentation fault. 0x00007fbc3336fb63 in ?? () fr ...

  10. Python字符串的修改以及传参

    前两天去面试web developer,面试官提出一个问题,用JavaScript或者Python实现字符串反转,我选择了Python,然后写出了代码(错误的): #!/usr/bin/env pyt ...