Esempio n. 1
0
        private static Dictionary <string, T> MergeDictionary <T>(DictionaryMergeContext <T> item, DictionaryMergeContext <T> overrideItem)
        {
            Dictionary <string, T> merged;

            if (overrideItem == null || overrideItem.Item == null)
            {
                merged = new Dictionary <string, T>();
            }
            else
            {
                merged = new Dictionary <string, T>(overrideItem.Item);
            }
            if (item == null || item.Item == null)
            {
                return(merged);
            }
            else
            {
                foreach (var pair in item.Item)
                {
                    if (merged.ContainsKey(pair.Key))
                    {
                        Logger.LogWarning($"Both {item.Name} and {overrideItem.Name} contain definition for \"{pair.Key}\", the one from \"{overrideItem.Name}\" overrides the one from \"{item.Name}\".");
                    }
                    else
                    {
                        merged[pair.Key] = pair.Value;
                    }
                }
            }
            return(merged);
        }
Esempio n. 2
0
        public static Dictionary <string, T> MergeDictionary <T>(DictionaryMergeContext <T> item, DictionaryMergeContext <T> overrideItem, Merger <T> merger)
        {
            if (merger == null)
            {
                throw new ArgumentNullException(nameof(merger));
            }

            Dictionary <string, T> merged;

            if (overrideItem?.Item == null)
            {
                merged = new Dictionary <string, T>();
            }
            else
            {
                merged = new Dictionary <string, T>(overrideItem.Item);
            }
            if (item?.Item == null)
            {
                return(merged);
            }
            else
            {
                foreach (var pair in item.Item)
                {
                    T value;
                    if (merged.TryGetValue(pair.Key, out value))
                    {
                        merged[pair.Key] = merger(pair.Key, new MergeContext <T>(item.Name, pair.Value), new MergeContext <T>(overrideItem.Name, value));
                    }
                    else
                    {
                        merged[pair.Key] = pair.Value;
                    }
                }
            }
            return(merged);
        }