代码改变世界

代码演示 .NET 4.5 自带的 ReadonlyCollection 的使用

2014-05-29 15:53  音乐让我说  阅读(980)  评论(0编辑  收藏  举报

代码如下:

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

 

 

谢谢浏览!