代码演示 .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,能方便的解释出我们的思路.很久之前我也写过一篇文章, ...
随机推荐
- Web端测试和移动端测试的区别
1.记录bug 在Web端可以通过系统自带的截图和QQ截图等方式来截取bug的图片,对于错误的地方可以用工具自带的标识来重点标记. 对于移动端设备可以用手机自带的截图工具来截图然后传到电脑上,个人一般 ...
- android 多布局
做为最后的方法,也是最后一个才会考虑的方法,那就是为不同的尺寸界面单独写布局.不到万不得已不要用这个方法,相信不少人和我一样都被逼着用过这个方法吧.需要说明的是,横竖屏切换使用不同布局也是用这个方法解 ...
- Tomcat之web项目部署
Tomcat一般用于部署JavaWeb项目. 遇到的问题 Linux操作系统中,在tomcat中部署项目时,一般只需要把项目war包:demo.war放到webapps下,然后启动tomcat即可.这 ...
- Delphi XE8,C++ Builder XE8,RAD Studio XE8 官方 ISO 文件下载,附激活工具
RAD Studio XE8 v22.0.19027.8951 官方ISO下载(6.72G):http://altd.embarcadero.com/download/radstudio/xe8/de ...
- phpmyadmin导入sql数据的纠结
今天准备往phpstudy的mysql导入数据,超过2M,提示" 没有接收到要导入的数据.可能是文件名没有提交,也可能是文件大小超出 PHP 限制.参见 FAQ 1.16. " 折 ...
- android sdk
android sdk 网站 为什么google 的网站都上不去. http://www.androiddevtools.cn/
- Android——程序移植 相关知识总结贴
android 移植笔记有感 http://www.apkbus.com/android-11842-1-1.html Android振动器系统结构和移植和调试 http://www.apkbus ...
- java中获取比毫秒更为精确的时间
所以这里提醒做非常精确的时间统计的朋友,谨慎使用System.currentTimeMillis() . 在Java中可以通过System.currentTimeMillis()或者System.na ...
- 用于主题检测的临时日志(b2d5c7b3-e3f6-4b0f-bfa4-a08e923eda9b - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
这是一个未删除的临时日志.请手动删除它.(1c773d57-4f35-40cf-ad62-bd757d5fcfae - 3bfe001a-32de-4114-a6b4-4005b770f6d7)
- 在JS中设置Select和radio选中
<select id="Gender" name="Gender"> <option value="1">男< ...