public void ShouldGetDefaultOnNullValueChangeTypeTick1Test() { int value; value = ReflectionExtensions.ChangeType <int>(null); Assert.AreEqual(default(int), value); }
public void ShouldGetDefaultOnNullValueChangeTypeTest() { object value; value = ReflectionExtensions.ChangeType(null, typeof(int)); Assert.AreEqual(default(int), value); }
public void ShouldGetNonNullOnNonNullValueNullableChangeTypeTick1Test() { int?value; value = ReflectionExtensions.ChangeType <int?>((byte)1); Assert.IsNotNull(value); Assert.IsInstanceOf <int?>(value); Assert.AreEqual((int?)1, value); }
public void ShouldGetNonNullOnNonNullValueChangeTypeTest() { object value; value = ReflectionExtensions.ChangeType((byte)1, typeof(int)); Assert.IsNotNull(value); Assert.IsInstanceOf <int>(value); Assert.AreEqual((int)1, value); }
protected virtual object CreateAndMap(Type t, XElement element) { object x; if (t == typeof(string)) { x = (object)element.Value; } else if (t.IsPrimitive) { x = ReflectionExtensions.ChangeType((object)element.Value, t, this.Culture); } else { x = Activator.CreateInstance(t); this.Map(x, element); } return(x); }
protected virtual object Map(object x, XElement root) { foreach (PropertyInfo prop in x.GetType().GetProperties()) { try { Type type = prop.PropertyType; if ((type.IsPublic || type.IsNestedPublic) && prop.CanWrite) { object[] customAttributes = prop.GetCustomAttributes(typeof(DeserializeAsAttribute), false); XName name = customAttributes.Length <= 0 ? XmlExtensions.AsNamespaced(prop.Name, this.Namespace) : XmlExtensions.AsNamespaced(((DeserializeAsAttribute)customAttributes[0]).Name, this.Namespace); object valueFromXml = this.GetValueFromXml(root, name, prop); if (valueFromXml == null) { if (type.IsGenericType) { Type t = type.GetGenericArguments()[0]; XElement elementByName = this.GetElementByName(root, (XName)t.Name); IList list = (IList)Activator.CreateInstance(type); if (elementByName != null && root != null) { IEnumerable <XElement> elements = root.Elements(elementByName.Name); this.PopulateListFromElements(t, elements, list); } prop.SetValue(x, (object)list, (object[])null); } } else { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { if (string.IsNullOrEmpty(valueFromXml.ToString())) { prop.SetValue(x, (object)null, (object[])null); continue; } type = type.GetGenericArguments()[0]; } if (type == typeof(bool)) { string s = valueFromXml.ToString().ToLower(); prop.SetValue(x, (object)(bool)(XmlConvert.ToBoolean(s) ? true : false), (object[])null); } else if (type.IsPrimitive) { prop.SetValue(x, ReflectionExtensions.ChangeType(valueFromXml, type, this.Culture), (object[])null); } else if (type.IsEnum) { object enumValue = ReflectionExtensions.FindEnumValue(type, valueFromXml.ToString(), this.Culture); prop.SetValue(x, enumValue, (object[])null); } else if (type == typeof(Uri)) { Uri uri = new Uri(valueFromXml.ToString(), UriKind.RelativeOrAbsolute); prop.SetValue(x, (object)uri, (object[])null); } else if (type == typeof(string)) { prop.SetValue(x, valueFromXml, (object[])null); } else if (type == typeof(DateTime)) { object obj = (object) (StringExtensions.HasValue(this.DateFormat) ? DateTime.ParseExact(valueFromXml.ToString(), this.DateFormat, (IFormatProvider)this.Culture) : DateTime.Parse(valueFromXml.ToString(), (IFormatProvider)this.Culture)); prop.SetValue(x, obj, (object[])null); } else if (type == typeof(DateTimeOffset)) { string str = valueFromXml.ToString(); if (!string.IsNullOrEmpty(str)) { try { DateTimeOffset dateTimeOffset = XmlConvert.ToDateTimeOffset(str); prop.SetValue(x, (object)dateTimeOffset, (object[])null); } catch (Exception ex) { object result; if (XmlDebuggingDeserializer.TryGetFromString(str, out result, type)) { prop.SetValue(x, result, (object[])null); } else { DateTimeOffset dateTimeOffset = DateTimeOffset.Parse(str); prop.SetValue(x, (object)dateTimeOffset, (object[])null); } } } } else if (type == typeof(Decimal)) { object obj = (object)Decimal.Parse(valueFromXml.ToString(), (IFormatProvider)this.Culture); prop.SetValue(x, obj, (object[])null); } else if (type == typeof(Guid)) { object obj = (object) (string.IsNullOrEmpty(valueFromXml.ToString()) ? Guid.Empty : new Guid(valueFromXml.ToString())); prop.SetValue(x, obj, (object[])null); } else if (type == typeof(TimeSpan)) { TimeSpan timeSpan = XmlConvert.ToTimeSpan(valueFromXml.ToString()); prop.SetValue(x, (object)timeSpan, (object[])null); } else if (type.IsGenericType) { Type t = type.GetGenericArguments()[0]; IList list = (IList)Activator.CreateInstance(type); XElement elementByName = this.GetElementByName(root, XmlExtensions.AsNamespaced(prop.Name, this.Namespace)); if (elementByName.HasElements) { XElement xelement = Enumerable.FirstOrDefault <XElement>(elementByName.Elements()); if (xelement != null) { IEnumerable <XElement> elements = elementByName.Elements(xelement.Name); this.PopulateListFromElements(t, elements, list); } } prop.SetValue(x, (object)list, (object[])null); } else if (ReflectionExtensions.IsSubclassOfRawGeneric(type, typeof(List <>))) { object obj = this.HandleListDerivative(root, prop.Name, type); prop.SetValue(x, obj, (object[])null); } else { object result; if (XmlDebuggingDeserializer.TryGetFromString(valueFromXml.ToString(), out result, type)) { prop.SetValue(x, result, (object[])null); } else if (root != null) { XElement elementByName = this.GetElementByName(root, name); if (elementByName != null) { object andMap = this.CreateAndMap(type, elementByName); prop.SetValue(x, andMap, (object[])null); } } } } } } catch (Exception e) { throw; } } return(x); }
public void ShouldFailOnNullTypeChangeTypeTest() { ReflectionExtensions.ChangeType(1, null); }