private static bool ConvertForDictionary(DictionaryDescriptor dictionaryDescriptor, ref object data) { object convertedDictionary; if (DictionaryDescriptor.IsDictionary(data.GetType())) { if (!TryConvertDictionaryData(data, dictionaryDescriptor, out convertedDictionary)) { return(false); } } else { var dataType = data.GetType(); var key = dataType.GetMember("Key").OfType <PropertyInfo>().FirstOrDefault()?.GetValue(data); if (key == null || !TypeConverterHelper.TryConvert(key, dictionaryDescriptor.KeyType, out key)) { return(false); } var value = dataType.GetMember("Value").OfType <PropertyInfo>().FirstOrDefault()?.GetValue(data); if (value == null || !TypeConverterHelper.TryConvert(value, dictionaryDescriptor.ValueType, out value)) { return(false); } convertedDictionary = Activator.CreateInstance(dictionaryDescriptor.Type, true); dictionaryDescriptor.SetValue(convertedDictionary, key, value); } data = convertedDictionary; return(true); }
/// <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); }
protected static object ConvertValue(object value, Type type) { if (value == null) { return(null); } object convertedValue; if (!TypeConverterHelper.TryConvert(value, type, out convertedValue)) { throw new InvalidOperationException("Can not convert value to the required type"); } return(convertedValue); }
/// <summary> /// Tries to convert the <paramref name="sourceCollection"/> to the type described by <paramref name="collectionDescriptor"/>. /// </summary> /// <param name="sourceCollection"></param> /// <param name="collectionDescriptor"></param> /// <param name="convertedCollection"></param> /// <returns><c>true</c> if the <paramref name="sourceCollection"/> could be converted to the type described by <paramref name="collectionDescriptor"/>; otherwise, <c>false</c>.</returns> private static bool TryConvertCollectionData([NotNull] object sourceCollection, [NotNull] CollectionDescriptor collectionDescriptor, out object convertedCollection) { try { var sourceCollectionType = sourceCollection.GetType(); // Already same type if (collectionDescriptor.Type == sourceCollectionType) { convertedCollection = sourceCollection; return(true); } convertedCollection = Activator.CreateInstance(collectionDescriptor.Type, true); var sourceCollectionDescriptor = (CollectionDescriptor)TypeDescriptorFactory.Default.Find(sourceCollectionType); foreach (var item in EnumerateCollection(sourceCollection, sourceCollectionDescriptor)) { object obj; if (!TypeConverterHelper.TryConvert(item, collectionDescriptor.ElementType, out obj)) { // (optimistic) try to convert the remaining items continue; } collectionDescriptor.Add(convertedCollection, obj); } return(collectionDescriptor.GetCollectionCount(convertedCollection) > 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 convertedCollection = null; return(false); }
private static bool ConvertForCollection(CollectionDescriptor collectionDescriptor, [NotNull] ref object data) { if (CollectionDescriptor.IsCollection(data.GetType())) { if (!TryConvertCollectionData(data, collectionDescriptor, out data)) { return(false); } } else { object convertedData; if (!TypeConverterHelper.TryConvert(data, collectionDescriptor.ElementType, out convertedData)) { return(false); } var convertedCollection = Activator.CreateInstance(collectionDescriptor.Type, true); collectionDescriptor.Add(convertedCollection, convertedData); data = convertedCollection; } return(true); }
private static bool ConvertForSet(SetDescriptor setDescriptor, ref object data) { if (SetDescriptor.IsSet(data.GetType())) { if (!TryConvertSetData(data, setDescriptor, out data)) { return(false); } } else { object convertedData; if (!TypeConverterHelper.TryConvert(data, setDescriptor.ElementType, out convertedData)) { return(false); } var convertedCollection = Activator.CreateInstance(setDescriptor.Type, true); setDescriptor.Add(convertedCollection, convertedData); data = convertedCollection; } return(true); }
private static bool ConvertForProperty(Type targetType, ref object data) { return(TypeConverterHelper.TryConvert(data, targetType, out data)); }