zoukankan      html  css  js  c++  java
  • 代码演示 .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=1,  Value="value1"});
                _mutableConfiguration.Add("key1", new ConfigElement { Id = 1, Value = "value1" });
                _mutableConfiguration["key2"] = new ConfigElement { Id = 1, 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 = 100, 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();
            }
        }
    }

    谢谢浏览!

  • 相关阅读:
    书单
    [转载] 修改WIN10的DNS、以及操作系统和 Web 浏览器清除和刷新 DNS 缓存方法汇总
    【题解】 【集训队作业2018】喂鸽子 minmax容斥+期望dp+补集转化 UOJ449
    【题解】 CF809E Surprise me! 虚树+莫比乌斯反演+狄利克雷卷积
    【题解】 CF1478E Nezzar and Binary String 线段树+时间逆序
    如何处理调用EasyCVR地址集成通过EasyPlayer播放器不能播放的问题?
    智慧能源:智能安防监控技术EasyCVR在石油能源行业中的场景应用
    网络穿透/动态组网工具EasyNTS报错connect refused该如何处理?
    如何处理C++编译webrtc无法成功获取sdp的问题?
    硬核讲解:编译webrtc协议为什么需要turn服务器?
  • 原文地址:https://www.cnblogs.com/Music/p/readonly-collection-in-dotnet4_5.html
Copyright © 2011-2022 走看看