/// <summary>
 ///     Writes the specified <see cref="IPlistSerializable" /> object to the given file path as a binary plist.
 /// </summary>
 /// <param name="path">The file path to write to.</param>
 /// <param name="obj">The <see cref="IPlistSerializable" /> object to write.</param>
 public void WriteObject(string path, IPlistSerializable obj)
 {
     using (var stream = File.Create(path))
     {
         WriteObject(stream, obj);
     }
 }
 /// <summary>
 /// Writes the specified <see cref="IPlistSerializable"/> object to the given file path as a binary plist.
 /// </summary>
 /// <param name="path">The file path to write to.</param>
 /// <param name="obj">The <see cref="IPlistSerializable"/> object to write.</param>
 public void WriteObject(string path, IPlistSerializable obj)
 {
     using (FileStream stream = File.Create(path))
     {
         this.WriteObject(stream, obj);
     }
 }
        /// <summary>
        ///     Writes the specified <see cref="IPlistSerializable" /> object to the given stream as a binary plist.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="obj">The <see cref="IPlistSerializable" /> object to write.</param>
        /// <param name="closeStream">A value indicating whether to close the stream after the write operation completes.</param>
        public void WriteObject(Stream stream, IPlistSerializable obj, bool closeStream)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj), "obj cannot be null.");
            }

            WriteObject(stream, obj.ToPlistDictionary(), closeStream);
        }
 /// <summary>
 ///     Writes the specified <see cref="IPlistSerializable" /> object to the given stream as a binary plist.
 /// </summary>
 /// <param name="stream">The stream to write to.</param>
 /// <param name="obj">The <see cref="IPlistSerializable" /> object to write.</param>
 public void WriteObject(Stream stream, IPlistSerializable obj)
 {
     WriteObject(stream, obj, true);
 }
        /// <summary>
        /// Writes the specified <see cref="IPlistSerializable"/> object to the given stream as a binary plist.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="obj">The <see cref="IPlistSerializable"/> object to write.</param>
        /// <param name="closeStream">A value indicating whether to close the stream after the write operation completes.</param>
        public void WriteObject(Stream stream, IPlistSerializable obj, bool closeStream)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj", "obj cannot be null.");
            }

            this.WriteObject(stream, obj.ToPlistDictionary(), closeStream);
        }
 /// <summary>
 /// Writes the specified <see cref="IPlistSerializable"/> object to the given stream as a binary plist.
 /// </summary>
 /// <param name="stream">The stream to write to.</param>
 /// <param name="obj">The <see cref="IPlistSerializable"/> object to write.</param>
 public void WriteObject(Stream stream, IPlistSerializable obj)
 {
     this.WriteObject(stream, obj, true);
 }
Exemple #7
0
        /// <summary>
        /// Gets the readable plist value of the given object identified by the specified type.
        /// </summary>
        /// <param name="type">The type the object is expected to have after being de-serialized.</param>
        /// <param name="obj">The raw plist object value.</param>
        /// <returns>A readable plist object value.</returns>
        private object GetReadablePlistObject(Type type, object obj)
        {
            object      result    = null;
            IDictionary plistDict = obj as IDictionary;

            if (obj != null)
            {
                if (typeof(IPlistSerializable).IsAssignableFrom(type))
                {
                    if (plistDict != null)
                    {
                        IPlistSerializable serResult = (IPlistSerializable)Activator.CreateInstance(type);
                        serResult.FromPlistDictionary(plistDict);
                    }
                }
                else if (typeof(IDictionary).IsAssignableFrom(type))
                {
                    if (plistDict != null)
                    {
                        Type keyType = typeof(object), valueType = typeof(object);

                        if (type.IsGenericType)
                        {
                            Type[] args = type.GetGenericArguments();
                            keyType   = args[0];
                            valueType = args[1];
                        }

                        IDictionary dictResult = (IDictionary)(type.IsInterface ?
                                                               Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(keyType, valueType)) :
                                                               Activator.CreateInstance(type));

                        foreach (object key in plistDict.Keys)
                        {
                            if (!type.IsGenericType)
                            {
                                keyType   = key.GetType();
                                valueType = plistDict[key] != null ? plistDict[key].GetType() : typeof(object);
                            }

                            dictResult[this.GetReadablePlistObject(keyType, key)] = this.GetReadablePlistObject(valueType, plistDict[key]);
                        }

                        result = dictResult;
                    }
                }
                else if (type.IsCollection())
                {
                    IEnumerable plistColl = obj as IEnumerable;

                    if (plistColl != null)
                    {
                        Type  valueType = typeof(object);
                        bool  isArray   = false;
                        IList listResult;

                        if (type.IsGenericType)
                        {
                            valueType = type.GetGenericArguments()[0];
                        }
                        else if (typeof(Array).IsAssignableFrom(type))
                        {
                            valueType = type.GetElementType();
                            isArray   = true;
                        }

                        if (isArray)
                        {
                            listResult = new ArrayList();
                        }
                        else
                        {
                            // TODO: The default DataContractSerializer uses an informal protocal requiring a method named "Add()"
                            // rather than requiring concrete collection types to implement IList.
                            listResult = (IList)(type.IsInterface ?
                                                 Activator.CreateInstance(typeof(List <>).MakeGenericType(valueType)) :
                                                 Activator.CreateInstance(type));
                        }

                        foreach (object value in plistColl)
                        {
                            listResult.Add(this.GetReadablePlistObject(valueType, value));
                        }

                        result = isArray ? ((ArrayList)listResult).ToArray() : listResult;
                    }
                }
                else if (type.IsPrimitiveOrEnum())
                {
                    result = obj;
                }
                else
                {
                    if (plistDict != null)
                    {
                        if (!this.typeCache.ContainsKey(type))
                        {
                            this.typeCache[type] = new TypeCacheItem(type);
                        }

                        TypeCacheItem cache = this.typeCache[type];
                        result = Activator.CreateInstance(type);

                        for (int i = 0; i < cache.Fields.Count; i++)
                        {
                            FieldInfo           field  = cache.Fields[i];
                            DataMemberAttribute member = cache.FieldMembers[i];

                            if (plistDict.Contains(member.Name))
                            {
                                field.SetValue(result, this.GetReadablePlistObject(field.FieldType, plistDict[member.Name]));
                            }
                        }

                        for (int i = 0; i < cache.Properties.Count; i++)
                        {
                            PropertyInfo        property = cache.Properties[i];
                            DataMemberAttribute member   = cache.PropertyMembers[i];

                            if (plistDict.Contains(member.Name))
                            {
                                property.SetValue(result, this.GetReadablePlistObject(property.PropertyType, plistDict[member.Name]), null);
                            }
                        }
                    }
                }
            }

            return(result);
        }