Search code examples
exceptionextension-methodsidictionary

How to add IDictionary extension method for Exception.Data


I'd like to create an extension method to the IDictionary collection Exception.Data that allows me to add an item to the dictionary without having to ensure the key is unique.

I can't get the extension method to show up.

    public static void AddUnique<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
    {

    }

You would use this like

exception.Data.AddUnique("key", value);

What am I doing wrong? Is this even possible?


Solution

  • The type in the extension method must exactly match the type of Exception.Data, which is System.Collections.IDictionary.

    System.Collections.Generic.IDictionary <> System.Collections.IDictionary

    System.Collections.IDictionary does not have type parameters, so the proper code would be

        public static void AddUnique<TKey, TValue>(this System.Collections.IDictionary dictionary, TKey key, TValue value)
        {
    
        }