代码演示 .NET 4.5 自带的 ReadonlyCollection 的使用
代码如下:
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 的使用的更多相关文章
- 代码详解:TensorFlow Core带你探索深度神经网络“黑匣子”
来源商业新知网,原标题:代码详解:TensorFlow Core带你探索深度神经网络“黑匣子” 想学TensorFlow?先从低阶API开始吧~某种程度而言,它能够帮助我们更好地理解Tensorflo ...
- 14种网页jQuery和css3特效插件代码演示
1.网页table增删样式代码 演示和下载地址 2.jQuery左右滑动幻灯片插件 演示和下载地址 3.jQuery文字轮播焦点图 演示和下载地址 4.网页文字焦点图切换 演示和下载地址 5.jQue ...
- 9种jQuery和css3图片动画特效代码演示
1.自由旋转的jQuery图片 演示和下载地址 2.css3阴影动画效果 演示和下载地址 3.拉窗帘特效图片 演示和下载地址 4.css3文字特效动画 演示和下载地址 5.css3时钟代码 演示和下载 ...
- java 覆盖hashCode()深入探讨 代码演示样例
java 翻盖hashCode()深入探讨 代码演示样例 package org.rui.collection2.hashcode; /** * 覆盖hashcode * 设计HashCode时最重要 ...
- javascript 压缩空格代码演示
压缩空格代码演示 主要是讲解 压缩一个字符串两段空格 例如:javascript函数里的空格不论是这样 var s = "Hello World ...
- 单元测试_JUnit常用单元测试注解介绍及代码演示
JUnit常用单元测试注解介绍及代码演示 by:授客 QQ:1033553122 1. 测试环境 1 2. 基础概念 1 3. 常用Annotation 1 4. 运行环境配置 3 maven配置 ...
- Java用代码演示String类中的以下方法的用法
用代码演示String类中的以下方法的用法 (1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true (2)char charAt(int index): 返回索引上 ...
- FFmpeg(9)-解码器解码代码演示(FFmpeg调用MediaCodec实现硬解码、多线程解码、及音视频解码性能测试)
一.AVFrame 用来存放解码后的数据. [相关函数] AVFrame *frame = av_frame_alloc(); // 空间分配,分配一个空间 ...
- 代码演示神器——jsfiddle
目录: 1. 介绍 2. jsfiddle的具体使用 3. 总结 1. 介绍 很多时候,我们需要在我们写的文章或博客中,即时显示出我们写的demo,能方便的解释出我们的思路.很久之前我也写过一篇文章, ...
随机推荐
- paip.花生壳 服务启动失败 以及不能安装服务,权限失败的解决
paip.花生壳 服务启动失败 以及不能安装服务,权限失败的解决 系统win7 NewPhDDNS_1.0.0.30166.exe 作者Attilax 艾龙, EMAIL:1466519819@ ...
- 493萬Gmail用戶的賬號密碼遭洩露,Google否認自己存在安全漏洞
最近,大公司在互聯網信息安全問題上狀況頻出.上週,蘋果因iCloud被黑客攻擊而導致大量明星私照外洩,著實是熱鬧了一陣.而Google也來湊熱鬧了.據俄羅斯媒體CNews消息,近493萬Gmail用戶 ...
- APP顶号逻辑
登录的接口login.do接口上需要记录关键的信息:userId.设备型号(Android|iPhone).登录时间.登录IP.设备唯一标识(UUID) 当在另外一台新设备上登录同一帐号时,将user ...
- chrome诡异的Provisional headers are shown
昨天吐槽了cocos2d-js的问题,所以就准备调研几个其它HTML5引擎,发现PIXI性能极高,但是没有音频.而Phaser.js是在PIXI.js的基础之上进行的封装.而国内有一家公司,开发一个叫 ...
- mssql 获取表空间大小
create table #Data(name varchar(100),row varchar(100),reserved varchar(100),data varchar(100),index_ ...
- eclipse 和 android studio 编译时报 Unsupported major.minor version 52.0错解决办法
version 52.0 是java8的环境.当gradle tools 升级到2.2.1时候,可能编译时候会报该错误. 很多网友说更改java version,但是很多时候无效.下面是我遇到时候的解 ...
- Java蛇形数组的简单实现代码
上周五和朋友聊天谈到个蛇形数组的java实现办法,命题是:假设一个二维数组宽w高h,从1开始蛇形输出. int[][] numberMatric = new int[w][h]; 当时午睡过头脑袋不清 ...
- Linux每天定时重启Tomcat服务
1:查看crond 服务状态(确认Linux任务计划服务开启) service crond status crond (pid 1937) is running... 2:编写重启Tomcat的sh ...
- Visual Studio 新建项目报错" this template attempted to load component assembly 'NuGet.VisualStudio.Interop, ….".
"Error: this template attempted to load component assembly 'NuGet.VisualStudio.Interop, Version ...
- Entity Framework 在Vs2012下Update Model From DataBase 失败的问题
http://stackoverflow.com/questions/13054212/vs-2012-ef-5-0-update-model-from-database-not-picking-up ...