/// <summary> /// De-serializes an object constant. /// </summary> /// <param name="element">The element from which to de-serialize the constant.</param> /// <param name="type">The type.</param> /// <returns>The de-serialized object constant.</returns> /// <exception cref="System.Runtime.Serialization.SerializationException">Expected type attribute.</exception> static object DeserializeCustom( XElement element, Type type) { if (element == null) { throw new ArgumentNullException(nameof(element)); } if (element.Elements().FirstOrDefault() == null) { return(null); } string typeString; var typeAttribute = element.Attribute(XNames.Attributes.Type); if (typeAttribute == null) { throw new SerializationException("Expected type attribute."); } typeString = typeAttribute.Value; if (typeString.StartsWith(anonymousTypePrefix, StringComparison.Ordinal)) { return(DeserializeAnonymous(element, type)); } var serializer = new DataContractSerializer(TypeNameResolver.GetType(typeString)); using (var reader = element.Elements().First().CreateReader()) return(serializer.ReadObject(reader)); }
internal static Type GetDataType(XElement element) { if (element == null) { throw new ArgumentNullException(nameof(element)); } var type = TypeNameResolver.GetType(element.Name.LocalName); if (type == null && element.Name == XNames.Elements.Anonymous) { type = DataSerialization.GetType(element); if (type == null) { throw new SerializationException("Expected constant element's type."); } } if (type == typeof(Nullable <>)) { Type valueType = GetType(element); if (valueType != null) { return(typeof(Nullable <>).MakeGenericType(valueType)); } var valueElement = element.Elements().FirstOrDefault(); if (valueElement == null) { return(null); } valueType = GetDataType(valueElement); if (valueType == null) { return(null); } return(typeof(Nullable <>).MakeGenericType(valueType)); } if (type == typeof(object)) { return(GetCustomConstantType(element)); } if (type == typeof(Enum)) { return(GetEnumConstantType(element)); } return(type); }
/// <summary> /// Gets the type corresponding to a type name written in an XML attribute. /// </summary> /// <param name="typeAttribute">The type attribute.</param> /// <returns>The specified type.</returns> internal static Type GetType(XAttribute typeAttribute) { if (typeAttribute == null) { return(null); } return(TypeNameResolver.GetType(typeAttribute.Value)); }
static Type GetEnumConstantType(XElement element) { if (element == null) { throw new ArgumentNullException(nameof(element)); } var typeAttribute = element.Attribute(XNames.Attributes.Type); if (typeAttribute == null) { throw new SerializationException("Expected type attribute."); } return(TypeNameResolver.GetType(typeAttribute.Value)); }