如题,如何自定义集合Dictionary<TKey,TValue>中的   TValue值被改变的事件,
class CommandText
    {
        public ArrayList text { get; set; }
        public Dictionary<string, string> textParams
        {
            get 
            {
                return value;
            }
            set
            {
                if (textParams != value)
                {
                    Host();
                    OnAssign();
                }
            }
        }        private delegate void textParamsEventHandler();
        private event textParamsEventHandler Assign;
        public void OnAssign()
        {
            if (this.Assign != null)
            {
                Assign();
            }
        }
        //注册事件
        public void Host()
        {
            Assign += new textParamsEventHandler(StructText);
        }
        protected void StructText()
        {
            foreach (KeyValuePair<string, string> textParam in textParams)
            {
                //根据ROS命令规则,过滤第一条
                for (int i = 1; i < text.Count; i++)
                {
                    //判断commandText是否存在该 参数
                    int index = text[i].ToString().IndexOf(textParam.Key);
                    if (index >= 0)
                    {
                        //获取旧值
                        string old = text[i].ToString().Substring(index + 1, text[i].ToString().Length - index - 1);
                        //替换新值
                        text[i].ToString().Replace(old, textParam.Value);
                    }
                }
            }
        }
    }请给出描述代码,不胜感激!给出 实例DEMO的,追加分数!

解决方案 »

  1.   

    class DictionaryWapper<TKey, TValue>
    {
        public class ValueChangedEventArgs<T> : EventArgs
        {
            public T Key { get; set; }
            public ValueChangedEventArgs(T key)
            {
                Key = key;
            }
        }
        private Dictionary<TKey, TValue> _dict;
        public event EventHandler OnValueChanged;
        public DictionaryWapper(Dictionary<TKey, TValue> dict)
        {
            _dict = dict;
        }
        public TValue this[TKey Key]
        {
            get { return _dict[Key]; }
            set 
            { 
                if (!_dict[Key].Equals(value)) 
                {
                    _dict[Key] = value;
                    OnValueChanged(this, new ValueChangedEventArgs<TKey>(Key));
                }
            }
        }
    }
      

  2.   

    谢谢caozhy的帮助,能够贴出你的调用代码吗
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var dict = Enumerable.Range(0, 10).ToDictionary(x => x, x => x.ToString());
                var dw = new DictionaryWapper<int, string>(dict);
                dw.OnValueChanged += new EventHandler<DictionaryWapper<int, string>.ValueChangedEventArgs<int>>(dw_OnValueChanged);
                dw[3] = "123";
            }        static void dw_OnValueChanged(object sender, DictionaryWapper<int, string>.ValueChangedEventArgs<int> e)
            {
                Console.WriteLine("Element {0} is changed.", e.Key);
            }
        }
        class DictionaryWapper<TKey, TValue>
        {
            public class ValueChangedEventArgs<T> : EventArgs
            {
                public T Key { get; set; }
                public ValueChangedEventArgs(T key)
                {
                    Key = key;
                }
            }
            private Dictionary<TKey, TValue> _dict;
            public event EventHandler<ValueChangedEventArgs<TKey>> OnValueChanged;
            public DictionaryWapper(Dictionary<TKey, TValue> dict)
            {
                _dict = dict;
            }
            public TValue this[TKey Key]
            {
                get { return _dict[Key]; }
                set
                {
                    if (!_dict[Key].Equals(value))
                    {
                        _dict[Key] = value;
                        OnValueChanged(this, new ValueChangedEventArgs<TKey>(Key));
                    }
                }
            }
        }
    }
      

  4.   

    问题已经解决了,非常感谢caozhy!!var dict = Enumerable.Range(0, 10).ToDictionary(x => x, x => x.ToString());关于这行,如果是通过 TKey 来 检索自定义的类,而TKey是些有具体含义的‘name’'e-mail',可以实现吗
      

  5.   

    ToDictionary方法有<string,string>的重载吗
      

  6.   


    明白了,非常感谢。我是一个有决心学好.net的菜鸟,caozhy大侠收我当徒弟吧,我交学费。