Example #1
0
        private static void CustomReadPropertiesFromElements(SerializationContext serializationContext, PersistentType element, XmlReader reader)
        {
            while (!serializationContext.Result.Failed && !reader.EOF && reader.NodeType == global::System.Xml.XmlNodeType.Element)
            {
                switch (reader.LocalName)
                {
                case "dataContract":
                    if (reader.IsEmptyElement)
                    {                                        // No serialized value, must be default one.
                        SerializationUtilities.Skip(reader); // Skip this tag.
                    }
                    else
                    {
                        DataContractDescriptor dataContractDescriptor = new DataContractDescriptor();
                        dataContractDescriptor.DeserializeFromXml(reader);
                        element.DataContract = dataContractDescriptor;

                        SerializationUtilities.SkipToNextElement(reader);
                        reader.SkipToNextElementFix();
                    }
                    break;

                default:
                    return;  // Don't know this element.
                }
            }
        }
        /// <summary>
        /// Prepare a data contract.
        /// </summary>
        /// <param name="type">The type to prepare.</param>
        /// <returns>An alias-contract pair.</returns>
        /// <exception cref="InvalidDataContractException">Type does not conform to data contract rules.
        /// For example, the DataContractAttribute attribute has not been applied to the type.</exception>
        private static KeyValuePair<string, DataContractDescriptor> PrepareDataContract(Type type)
        {
            if (type == null) throw new ArgumentNullException("type");

            try
            {
                bool isDataContract;
                var amfxType = GetAmfxType(type, out isDataContract);
                var alias = isDataContract
                    ? DataContractHelper.GetContractAlias(type)
                    : type.FullName;

                DataContractDescriptor descriptor;

                if (type.IsEnum)
                {
                    descriptor = new EnumDescriptor
                                     {
                                         Values = DataContractHelper.GetEnumValues(type)
                                     };

                }
                else
                {
                    descriptor = new DataContractDescriptor();
                }

                descriptor.Alias = alias;
                descriptor.Type = type;
                descriptor.IsPrimitive = !isDataContract;
                descriptor.AmfxType = amfxType;

                if (isDataContract)
                {
                    descriptor.FieldMap = DataContractHelper.GetContractFields(type);
                    descriptor.PropertyMap = DataContractHelper.GetContractProperties(type);
                }

                return new KeyValuePair<string, DataContractDescriptor>(alias, descriptor);
            }
            catch (Exception e)
            {
                throw new InvalidDataContractException(string.Format("Type '{0}' is not a valid data contract.", type.FullName), e);
            }
        }