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();
            }
        }
    }

    谢谢浏览!

  • 相关阅读:
    Bete冲刺第二阶段
    Beta版本冲刺计划及安排
    Bete冲刺第一阶段
    软件工程实践总结
    用户试用体验报告
    Bata版本冲刺计划及安排
    scrum阶段总结
    关于 微软必应词典客户端 的案例分析
    第三次结对编程
    结对项目之需求分析与原型模型设计
  • 原文地址:https://www.cnblogs.com/Music/p/readonly-collection-in-dotnet4_5.html
Copyright © 2011-2022 走看看