public void ObjectExtenderContainerConverterConvertToTypeCompliantWithXmlSerializer() { MockServiceProvider mockServiceProvider = new MockServiceProvider(); MockServiceProviderService mockService = new MockServiceProviderService(); mockServiceProvider.AddService(typeof(IExtensionProviderService), mockService); ObjectExtenderContainerConverter converter = new ObjectExtenderContainerConverter(mockServiceProvider); TestSerializableObject testObject1 = new TestSerializableObject(); testObject1.ValueOne = "TestDataOne"; testObject1.ValueTwo = 33; string stringRepresentation; Type[] types = { typeof(TestSerializableObject) }; ObjectExtenderContainer container1 = new ObjectExtenderContainer(); container1.ObjectExtenders.Add(testObject1); stringRepresentation = GenericSerializer.Serialize <ObjectExtenderContainer>(container1, types); string stringRepresentation2 = converter.ConvertTo(stringRepresentation, typeof(string)) as string; Assert.AreEqual(stringRepresentation, stringRepresentation2, "Not equal"); }
public void ObjectExtenderContainerConverterConvertFromTypeCompliantWithXmlSerializer() { MockServiceProvider mockServiceProvider = new MockServiceProvider(); Type[] types = { typeof(TestSerializableObject) }; MockServiceProviderService mockService = new MockServiceProviderService(); mockService.ObjectExtenderTypes.Add(types[0]); mockServiceProvider.AddService(typeof(IExtensionProviderService), mockService); ObjectExtenderContainerConverter converter = new ObjectExtenderContainerConverter(mockServiceProvider); TestSerializableObject testObject1 = new TestSerializableObject(); testObject1.ValueOne = "TestDataOne"; testObject1.ValueTwo = 33; string stringRepresentation; ObjectExtenderContainer container1 = new ObjectExtenderContainer(); container1.ObjectExtenders.Add(testObject1); stringRepresentation = GenericSerializer.Serialize <ObjectExtenderContainer>(container1, types); ObjectExtenderContainer container2 = converter.ConvertFrom(stringRepresentation) as ObjectExtenderContainer; Assert.IsNotNull(container2, "container is null"); Assert.IsNotNull(container2.ObjectExtenders, "ObjectExtenders is null"); Assert.AreEqual(1, container2.ObjectExtenders.Count, "container.ObjectExtenders.Count != 1"); TestSerializableObject testObject2 = container2.ObjectExtenders[0] as TestSerializableObject; Assert.AreEqual(testObject1.ValueOne, testObject2.ValueOne, "Not equal"); Assert.AreEqual(testObject1.ValueTwo, testObject2.ValueTwo, "Not equal"); }
/// <summary> /// Converts the given value object to the specified type, using the specified context and culture information. /// </summary> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param> /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo"></see>. If null is passed, the current culture is assumed.</param> /// <param name="value">The <see cref="T:System.Object"></see> to convert.</param> /// <param name="destinationType">The <see cref="T:System.Type"></see> to convert the value parameter to.</param> /// <returns> /// An <see cref="T:System.Object"></see> that represents the converted value. /// </returns> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception> /// <exception cref="T:System.ArgumentNullException">The destinationType parameter is null. </exception> public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { ObjectExtenderContainer objectExtenderContainer = value as ObjectExtenderContainer; if (destinationType == typeof(string) && objectExtenderContainer != null) { //We need to obtain all posible referenced types for the XMLSerializer (XMLInclude) IList <Type> types = GetTypesFromContainer(objectExtenderContainer); return(GenericSerializer.Serialize <ObjectExtenderContainer>(value, types)); } return(base.ConvertTo(context, culture, value, destinationType)); }
private IList <Type> GetTypesFromContainer(ObjectExtenderContainer objectExtenderContainer) { Guard.ArgumentNotNull(objectExtenderContainer, "objectExtenderContainer"); IList <Type> types = new List <Type>() { typeof(System.ComponentModel.BindingList <string>) }; foreach (object objectExtender in objectExtenderContainer.ObjectExtenders) { types.Add(objectExtender.GetType()); } return(types); }
/// <summary> /// Converts the given object to the type of this converter, using the specified context and culture information. /// </summary> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param> /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo"></see> to use as the current culture.</param> /// <param name="value">The <see cref="T:System.Object"></see> to convert.</param> /// <returns> /// An <see cref="T:System.Object"></see> that represents the converted value. /// </returns> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception> public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { ObjectExtenderContainer container = new ObjectExtenderContainer(); string convertValue = value as string; if (!string.IsNullOrEmpty(convertValue)) { try { //We need to obtain all posible referenced types for the XMLSerializer (XMLInclude) IList <Type> types = GetExtraTypesFromProviders(context); container = GenericSerializer.Deserialize <ObjectExtenderContainer>(convertValue, types); } catch (Exception ex) { Logger.Write(ex); } } return(container); }