コード例 #1
0
        private static void SerializeDictionary(Tag tag, IDictionary dictionary, string customItemName)
        {
            bool simpleType1 = false;

            foreach (object index in (IEnumerable)dictionary.Keys)
            {
                SdlSerializer.TryCoerce(index, out simpleType1);
                if (simpleType1)
                {
                    SdlSerializer.TryCoerce(dictionary[index], out simpleType1);
                    if (!simpleType1)
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }
            if (simpleType1)
            {
                foreach (object index1 in (IEnumerable)dictionary.Keys)
                {
                    bool   simpleType2;
                    string index2 = SdlSerializer.TryCoerce(index1, out simpleType2).ToString();
                    object obj    = SdlSerializer.TryCoerce(dictionary[index1], out simpleType2);
                    tag[index2] = obj;
                }
            }
            else
            {
                System.Type[] genericArguments = dictionary.GetType().GetGenericArguments();
                System.Type   type1            = genericArguments[0];
                System.Type   type2            = genericArguments[1];
                foreach (object index in (IEnumerable)dictionary.Keys)
                {
                    object instance = dictionary[index];
                    bool   simpleType2;
                    object obj  = SdlSerializer.TryCoerce(index, out simpleType2);
                    Tag    tag1 = new Tag(SdlSerializer.LowerCamelCase(customItemName ?? type2.Name));
                    tag.AddChild(tag1);
                    if (simpleType2)
                    {
                        tag1["key"] = obj;
                        SdlSerializer.SerializeInternal(instance, type2, tag1);
                    }
                    else
                    {
                        SdlSerializer.SerializeChild("key", index, type1, false, SdlSerializer.DefaultAttribute, tag1);
                        bool simpleType3;
                        SdlSerializer.SerializeChild("value", SdlSerializer.TryCoerce(instance, out simpleType3), type2, simpleType3, SdlSerializer.DefaultAttribute, tag1);
                    }
                }
            }
        }
コード例 #2
0
        private static void SerializeInternal(object instance, System.Type declaredType, Tag tag)
        {
            System.Type type = declaredType;
            TypeSerializationAttribute serializationAttribute = ReflectionHelper.GetFirstAttribute <TypeSerializationAttribute>(type) ?? new TypeSerializationAttribute();

            foreach (MemberInfo memberInfo in ReflectionHelper.GetSerializableMembers(type))
            {
                SerializationAttribute attr = ReflectionHelper.GetFirstAttribute <SerializationAttribute>(memberInfo) ?? new SerializationAttribute();
                if (!attr.Ignore)
                {
                    System.Type memberType = ReflectionHelper.GetMemberType(memberInfo);
                    object      obj1       = ReflectionHelper.GetValue(memberInfo, instance);
                    if (!attr.Optional || serializationAttribute.FlattenToList || obj1 != null && (!attr.DefaultValueOptional || !SdlSerializer.IsDefault(obj1)))
                    {
                        bool   simpleType;
                        object obj2 = SdlSerializer.TryCoerce(obj1, out simpleType);
                        if ((serializationAttribute.FlattenToList || attr.UseAttribute) && !simpleType)
                        {
                            throw new SdlSerializationException(Resources.SimpleTypeRequired, type, memberInfo);
                        }
                        if (serializationAttribute.FlattenToList)
                        {
                            tag.AddValue(obj2);
                        }
                        else
                        {
                            string name = SdlSerializer.LowerCamelCase(attr.Name ?? memberInfo.Name);
                            if (attr.UseAttribute)
                            {
                                tag[name] = obj2;
                            }
                            else
                            {
                                SdlSerializer.SerializeChild(name, obj2, memberType, simpleType, attr, tag);
                            }
                        }
                    }
                }
            }
        }
コード例 #3
0
        private static void SerializeCollection(Tag tag, IEnumerable collection, string customItemName)
        {
            List <object> list = new List <object>();

            System.Type type1       = collection.GetType();
            System.Type type2       = type1.IsArray ? type1.GetElementType() : type1.GetGenericArguments()[0];
            bool        simpleType1 = false;

            foreach (object obj1 in collection)
            {
                object obj2 = SdlSerializer.TryCoerce(obj1, out simpleType1);
                if (simpleType1)
                {
                    list.Add(obj2);
                }
                else
                {
                    break;
                }
            }
            if (simpleType1)
            {
                foreach (object obj in list)
                {
                    tag.AddValue(obj);
                }
            }
            else
            {
                foreach (object obj in collection)
                {
                    bool simpleType2;
                    SdlSerializer.SerializeChild(SdlSerializer.LowerCamelCase(customItemName ?? type2.Name), SdlSerializer.TryCoerce(obj, out simpleType2), type2, simpleType2, SdlSerializer.DefaultAttribute, tag);
                }
            }
        }