代码如下:

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.取当天记录的方法sql跟hql hibernate

    paip.取当天记录的方法sql跟hql hibernate #------两个方法...函数法和日期计算法.. 函数法: DATEDIFF(d,createTime,GETDATE())=0   / ...

  2. iOS开发----三目运算符

    一.三目运算符 1.基本格式 : (关系表达式) ? 表达式1 : 表达式2;  执行流程 : 关系表达式为 真 返回表达式1 关系表达式为假 返回表达式2 2.写一个例子来看一下三目运算符的使用: ...

  3. VS 2008 创建MFC程序对话框的步骤

    用过不少编程语言,可是刚开始学的时候最容易忘记一些简单的流程或者生疏了.那么这里就说说VS 2008 创建MFC程序对话框的步骤.我主要是android开发方面的.平时使用jni调用不少c++代码.所 ...

  4. javascript设计模式与开发实践阅读笔记(9)——命令模式

    命令模式:有时候需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是什么,此时希望用一种松耦合的方式来设计软件,使得请求发送者和请求接收者能够消除彼此之间的耦合关系. 说法很复 ...

  5. [原创]配置管理技术圈QQ群:129489184

    [原创]配置管理技术圈QQ群:129489184 配置管理技术圈QQ群:129489184,研究cvs,svn,git,cc等平台配置技术,涉及版本控制,持续集成,自动化构建等! 欢迎各位同学来,来时 ...

  6. 将数据导出到Excel2007格式。

    增加数据格式 public static void TableToExcel2(DataTable table, string filename, string sheetname) { XSSFWo ...

  7. [3].jekyll的基础

    一.创建新项目 以下是一个获取最简单 Jekyll 模板并生成静态页面的方法.: Administrator@FANGPENG /e $ jekyll new myblog # 创建名为 myblog ...

  8. Git – fatal: Unable to create ‘/.git/index.lock’: File exists错误解决办法

    有时候在提交的时候,中间提交出错,导致有文件被lock,所以会报下面的错误: fatal: Unable to create ‘/msg/.git/index.lock’: File exists. ...

  9. Windows 10开启默认网络驱动器访问

    在Windows 10的系统策略中,驱动器盘符的网络访问是默认关闭的,用管理员权限打开注册表,找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curre ...

  10. C#==>匿名方法 【转】

    http://blog.csdn.net/gishero/article/details/5161826 1,匿名方法 C#为委托提供一种机制,可以为委托定义匿名方法,匿名方法没有名称,编译器会定指定 ...