Override Dictionary.Add

前端 未结 5 1913
星月不相逢
星月不相逢 2020-12-17 10:07

I need to know how to override the Add-method of a certain Dictionary in a certain static class. Any suggestions?

If it matters, the dictionary looks like this:

5条回答
  •  太阳男子
    2020-12-17 10:58

    Another "quick and dirty" fix

    If you need to add some extra logic to the Add method, you can create your own method, with a slightly different signature, and use THAT in your code.

    public class MyOwnDictionary: Dictionary
    {
        public void AddPlus(TKey key, TValue value)
        {
            //some custom logic here
    
            base.Add(key,value);
        }
    }
    

    (but this can lead to maintenance difficulties)

提交回复
热议问题