System.Runtime.Serialization.Plists.DataContractBinaryPlistSerializer.GetWritablePlistObject C# (CSharp) Method

GetWritablePlistObject() private method

Gets the writable plist value of the given object identified by the specified type.
private GetWritablePlistObject ( Type type, object obj ) : object
type System.Type The of the object.
obj object The object to get the plist value of.
return object
        private object GetWritablePlistObject(Type type, object obj)
        {
            object result = null;

            if (obj != null)
            {
                if (typeof(IPlistSerializable).IsAssignableFrom(type))
                {
                    result = ((IPlistSerializable)obj).ToPlistDictionary();
                }
                else if (typeof(IDictionary).IsAssignableFrom(type))
                {
                    IDictionary dict = obj as IDictionary;
                    Dictionary<object, object> resultDict = new Dictionary<object, object>();

                    foreach (object key in dict.Keys)
                    {
                        object value = dict[key];
                        resultDict[this.GetWritablePlistObject(key.GetType(), key)] = this.GetWritablePlistObject(value.GetType(), value);
                    }

                    result = resultDict;
                }
                else if (type.IsCollection())
                {
                    IEnumerable coll = obj as IEnumerable;
                    List<object> resultColl = new List<object>();

                    foreach (object value in coll)
                    {
                        resultColl.Add(this.GetWritablePlistObject(value.GetType(), value));
                    }

                    result = resultColl;
                }
                else if (type.IsPrimitiveOrEnum())
                {
                    result = obj;
                }
                else
                {
                    if (!this.typeCache.ContainsKey(type))
                    {
                        this.typeCache[type] = new TypeCacheItem(type);
                    }

                    TypeCacheItem cache = this.typeCache[type];
                    Dictionary<string, object> resultDict = new Dictionary<string, object>();

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

                        if (member.EmitDefaultValue || !field.FieldType.IsDefaultValue(fieldValue))
                        {
                            resultDict[member.Name] = this.GetWritablePlistObject(field.FieldType, fieldValue);
                        }
                    }

                    for (int i = 0; i < cache.Properties.Count; i++)
                    {
                        PropertyInfo property = cache.Properties[i];
                        DataMemberAttribute member = cache.PropertyMembers[i];
                        object propertyValue = property.GetValue(obj, null);

                        if (member.EmitDefaultValue || !property.PropertyType.IsDefaultValue(propertyValue))
                        {
                            resultDict[member.Name] = this.GetWritablePlistObject(property.PropertyType, propertyValue);
                        }
                    }

                    result = resultDict;
                }
            }

            return result;
        }