• 代码演示 .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();
            }
        }
    }

    谢谢浏览!

  • 相关阅读:
    mysql,SQL标准,多表查询中内连接,外连接,自然连接等详解之查询结果集的笛卡尔积的演化
    java:JDBC详解
    卷积在深度学习中的作用(转自http://timdettmers.com/2015/03/26/convolution-deep-learning/)
    卷积(转自wiki百科)
    windows10环境下安装Tensorflow
    3、继承与派生
    2、对象和类
    1、从C语言到C++
    使用Jupyter Notebook编写技术文档
    3、利用GDB进行程序调试
  • 原文地址:https://www.cnblogs.com/Music/p/readonly-collection-in-dotnet4_5.html
Copyright © 2020-2023  润新知