代码演示 .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,能方便的解释出我们的思路.很久之前我也写过一篇文章, ...
随机推荐
- C#:使用MD5对用户密码加密与解密
C#中常涉及到对用户密码的加密于解密的算法,其中使用MD5加密是最常见的的实现方式.本文总结了通用的算法并结合了自己的一点小经验,分享给大家. 一.使用16位.32位.64位MD5方法对用户名加密 1 ...
- Maven学习总结(五)——聚合与继承
一.聚合 如果我们想一次构建多个项目模块,那我们就需要对多个项目模块进行聚合 1.1.聚合配置代码 <modules> <module>模块一</module> & ...
- Linux中如何产生core文件?
在程序不寻常退出时,内核会在当前工作目录下生成一个core文件(是一个内存映像,同时加上调试信息).使用gdb来查看core文件,可以指示出导致程序出错的代码所在文件和行数. 1.core文件 ...
- Android MultiDex兼容包怎么使用?
在Android系统中安装应用的时候,需要对Dex进行优化,但由于其处理工具DexOpt的限制,导致其id的数目不能够超过65536个.而MultiDex兼容包的出现,就很好的解决了这个问题,它可以配 ...
- 关于启明星系统(OA系统,预定系统,请假系统等)安全性的说明
启明星系统推荐是安装在内网里,因此,系统采用了较为简单的身份验证. 事实上,在访问页面时,系统会判断当前cookie的uid是否大于0,如果是,则表示用户已经登录,否则表示未登录. public st ...
- 用DOS批处理实现FTP自动上传、下载、清理文件
用DOS批处理实现FTP自动上传.下载.清理文件 最近好像特别的忙,好久没来写点东西了,今天写了一个利用批处理程序完成FTP自动上传.下载.清理文件的程序.赶紧 记录下来,以备日后之用.功能介绍:自动 ...
- linux C++ 获取文件绝对路径
提供ftp服务时需要获取文件绝对路径,这里记录一下. #include <stdlib.h> #include <stdio.h> #include <limits.h& ...
- activemq安装和启动
安装环境:linux redhat activemq版本:5.8.0 1.从http://activemq.apache.org/download.html地址下载apache-activemq-5. ...
- 利用 Python 只连接一次 MySQL
Github 地址 项目背景 最近做个项目,需要进行试驾分析,所谓"试驾",是指顾客在 4S 店指定人员的陪同下,沿着指定的路线驾驶车辆,从而了解这款汽车的行驶性能和操控性能.通常 ...
- 正则指引-量词demo
class Program { static void Main(string[] args) { string str = "1\"3"; var re1 = Rege ...