Example #1
0
        /// <summary>
        /// Tries to convert the <paramref name="sourceDictionary"/> to the type described by <paramref name="dictionaryDescriptor"/>.
        /// </summary>
        /// <param name="sourceDictionary"></param>
        /// <param name="dictionaryDescriptor"></param>
        /// <param name="convertedDictionary"></param>
        /// <returns><c>true</c> if the <paramref name="sourceDictionary"/> could be converted to the type described by <paramref name="dictionaryDescriptor"/>; otherwise, <c>false</c>.</returns>
        private static bool TryConvertDictionaryData([NotNull] object sourceDictionary, [NotNull] DictionaryDescriptor dictionaryDescriptor, out object convertedDictionary)
        {
            try
            {
                var sourceDictionaryType = sourceDictionary.GetType();
                // Already same type
                if (dictionaryDescriptor.Type == sourceDictionary.GetType())
                {
                    convertedDictionary = sourceDictionary;
                    return(true);
                }

                convertedDictionary = Activator.CreateInstance(dictionaryDescriptor.Type, true);
                var sourceDictionaryDescriptor = (DictionaryDescriptor)TypeDescriptorFactory.Default.Find(sourceDictionaryType);
                foreach (var k in sourceDictionaryDescriptor.GetKeys(sourceDictionary))
                {
                    var key = k;
                    if (!TypeConverterHelper.TryConvert(key, dictionaryDescriptor.KeyType, out key))
                    {
                        // (optimistic) try to convert the remaining items
                        continue;
                    }
                    var value = sourceDictionaryDescriptor.GetValue(sourceDictionary, k);
                    if (!TypeConverterHelper.TryConvert(value, dictionaryDescriptor.ValueType, out value))
                    {
                        // (optimistic) try to convert the remaining items
                        continue;
                    }
                    dictionaryDescriptor.SetValue(convertedDictionary, key, value);
                }
                return(dictionaryDescriptor.GetKeys(convertedDictionary)?.Count > 0);
            }
            catch (InvalidCastException) { }
            catch (InvalidOperationException) { }
            catch (FormatException) { }
            catch (NotSupportedException) { }
            catch (Exception ex) when(ex.InnerException is InvalidCastException)
            {
            }
            catch (Exception ex) when(ex.InnerException is InvalidOperationException)
            {
            }
            catch (Exception ex) when(ex.InnerException is FormatException)
            {
            }
            catch (Exception ex) when(ex.InnerException is NotSupportedException)
            {
            }

            // Incompatible type and no conversion available
            convertedDictionary = null;
            return(false);
        }