I am looking to implement a method that is capable of merging 2 dictionaries using generics. I've seen several great answers on SO already, but none handle the case of nested dictionaries. As in, what if the value of the dictionaries is another dictionary.In order to tackle this, I created the following method:using System;using System.Collections.Generic;using System.Reflection;namespace CollectionHelpers{   /// <summary>   /// This class contains helpful methods for manipulating collections.   /// Utilizes .NET 4.0 features.   /// </summary>   public class DictionaryHelper   {       #region Dictionary       /// <summary>       /// Unionise two dictionaries of generic types.       /// Duplicates take their value from the leftmost dictionary.       /// </summary>       /// <typeparam name=T1>Generic key type</typeparam>       /// <typeparam name=T2>Generic value type</typeparam>       /// <param name=D1>Dictionary 1</param>       /// <param name=D2>Dictionary 2</param>       /// <returns>The combined dictionaries.</returns>       public static Dictionary<T1, T2> UnionDictionaries<T1, T2>(Dictionary<T1, T2> D1, Dictionary<T1, T2> D2)       {           Dictionary<T1, T2> rd = new Dictionary<T1, T2>(D1);           foreach (var key in D2.Keys)           {               if (!rd.ContainsKey(key))                   rd.Add(key, D2[key]);               else if(rd[key].GetType().IsGenericType)               {                   if (rd[key].GetType().GetGenericTypeDefinition() == typeof(Dictionary<,>))                   {                       var mBase = MethodBase.GetCurrentMethod();                       MethodInfo info = mBase is MethodInfo ? (MethodInfo)mBase : typeof(DictionaryHelper).GetMethod(UnionDictionaries, BindingFlags.Public | BindingFlags.Static);                       var genericMethod = info.MakeGenericMethod(rd[key].GetType().GetGenericArguments()[0], rd[key].GetType().GetGenericArguments()[1]);                       var invocationResult = genericMethod.Invoke(null, new object[] { rd[key], D2[key] });                       rd[key] = (T2)invocationResult;                   }               }           }           return rd;       }       #endregion   }}The code works for the samples I've thrown at it so far. Obviously I can't test all cases. This is my attempt to improve on a previous design which used IDictionary, but that function is not safe since it doesn't check the actual types of the dictionaries it handles.    /// <summary>    /// Merge two IDictionaries.    /// If the values of two matching keys are Ilists then they will be merged without duplicates as well.    /// If not then the value of the leftmost targetDictionary will be maintained.    /// </summary>    /// <param name=targetDictionary>The dictionary to merge all values in to.</param>    /// <param name=sourceDictionary>The dictionary to merge values from.</param>    /// <returns></returns>    public static IDictionary MergeCollections(IDictionary targetDictionary, IDictionary sourceDictionary)    {        foreach (var key in sourceDictionary.Keys)        {            if (!targetDictionary.Contains(key))                targetDictionary.Add(key, sourceDictionary[key]);            else            {                if(targetDictionary[key] is IDictionary)                    targetDictionary[key] = MergeCollections((IDictionary)targetDictionary[key], (IDictionary)sourceDictionary[key]);                else if (targetDictionary[key] is IList)                    targetDictionary[key] = MergeCollections((IList)targetDictionary[key], (IList)sourceDictionary[key]);            }        }        return targetDictionary;    }The overloaded MergeCollections method with IList parameters doesn't matter right now.Which one of these would you prefer and why? Suggestions for improvements and other ways of tackling this problem are welcome.
Recursively merge dictionaries with generic types in C#
c#;.net;generics;hash table;reflection