コード例 #1
0
ファイル: CascadeSerializer.cs プロジェクト: Isuroku/Cascade
        void SerializeGenericDictionary(object instance, Type declaredType, IKey inKey, int inInheriteDeep, ILogPrinter inLogger)
        {
            var dictionary = instance as IDictionary;

            Type[] gen_args = declaredType.GetGenericArguments();
            if (gen_args.Length < 2)
            {
                LogError(inLogger, string.Format("SerializeGenericDictionary: Generic Arguments are None. Type {0}. Instance {1}", declaredType.Name, instance));
                return;
            }

            Type keyDeclaredType   = gen_args[0];
            Type valueDeclaredType = gen_args[1];

            if (!keyDeclaredType.IsAtomic())
            {
                LogError(inLogger, "Dictionary must simple key.");
            }
            else
            {
                IKey tree_key = inKey;
                if (tree_key.GetChildCount() > 0)
                {
                    tree_key = inKey.CreateChildKey("BaseDictionary");
                }

                foreach (var key in dictionary.Keys)
                {
                    IKey child = tree_key.CreateChildKey(key.ToString());
                    Serialize(dictionary[key], valueDeclaredType, child, 0, inLogger);
                }
            }
        }
コード例 #2
0
ファイル: CascadeSerializer.cs プロジェクト: Isuroku/Cascade
        void SerializeGenericCollection(object instance, Type type, IKey inKey, int inInheriteDeep, ILogPrinter inLogger)
        {
            var collection = instance as IEnumerable;

            Type[] gen_args = type.GetGenericArguments();
            if (gen_args.Length == 0)
            {
                LogError(inLogger, string.Format("SerializeGenericCollection: Generic Arguments are None. Type {0}. Instance {1}", type.Name, instance));
                return;
            }

            Type declaredItemType = gen_args[0];
            bool atomic_member    = declaredItemType.IsAtomic();

            IKey tree_key = inKey;

            if (tree_key.GetValuesCount() > 0 || tree_key.GetChildCount() > 0)
            {
                tree_key = inKey.CreateChildKey("BaseCollection");
            }

            if (atomic_member)
            {
                foreach (var item in collection)
                {
                    AddValueToKey(tree_key, item);
                }
            }
            else
            {
                foreach (var item in collection)
                {
                    IKey child = tree_key.CreateArrayKey();
                    Serialize(item, declaredItemType, child, 0, inLogger);
                }
            }
        }
コード例 #3
0
ファイル: CascadeSerializer.cs プロジェクト: Isuroku/Cascade
        void SerializeClass(object instance, Type type, IKey inKey, int inInheriteDeep, ILogPrinter inLogger)
        {
            MethodInfo mi = type.GetMethod("SerializationToCscd", new Type[] { typeof(CascadeParser.IKey), typeof(CascadeParser.ILogPrinter) });

            if (mi != null)
            {
                if (string.IsNullOrEmpty(inKey.GetName()))
                {
                    inKey.SetName("Value");
                }

                mi.Invoke(instance, new object[] { inKey, inLogger });
                return;
            }

            MemberInfo[] member_infos = _reflectionProvider.GetSerializableMembers(type);
            foreach (MemberInfo memberInfo in member_infos)
            {
                object value = _reflectionProvider.GetValue(memberInfo, instance);
                if (value == null)
                {
                    continue;
                }

                SCustomMemberParams member_params = GetMemberParams(memberInfo);

                if (member_params.DefaultValue != null)
                {
                    if (member_params.DefaultValue.GetType() != value.GetType())
                    {
                        LogError(inLogger, string.Format("DefaultValue and member {2} of class {3} have difference types: {0} and {1}",
                                                         member_params.DefaultValue.GetType().Name,
                                                         value.GetType().Name,
                                                         member_params.Name,
                                                         type.Name));
                    }
                    else if (member_params.DefaultValue.Equals(value))
                    {
                        continue;
                    }
                }
                else if (ReflectionHelper.IsDefault(value, _reflectionProvider, inLogger))
                {
                    continue;
                }


                IKey child = inKey.CreateChildKey(member_params.ChangedName);

                Type real_type   = value.GetType();
                Type member_type = memberInfo.GetMemberType();

                if (member_params.Converter != null && member_params.Converter.CanConvert(real_type))
                {
                    member_params.Converter.WriteKey(child, value, inLogger);
                }
                else
                {
                    Serialize(value, real_type, child, 0, inLogger);
                }

                // Write the runtime type if different (except nullables since they get unboxed)
                if (real_type != member_type && !member_type.IsNullable())
                {
                    IKey obj_type_key = child.CreateChildKey("RealObjectType");
                    obj_type_key.AddValue(real_type.FullName);
                    obj_type_key.AddValue(real_type.Assembly.FullName);
                }
            }
        }