예제 #1
0
        private object serializeValue(MemberInfo memberInfo, Type memberType, object item, int arrayDimension = 0)
        {
            if (item == null)
            {
                return(string.Empty);
            }

            IModel itemAsImodel = item as IModel;

            if (itemAsImodel != null)
            {
                return(itemAsImodel.ID);
            }

            if (memberType.IsArray && (item is Array))
            {
                Array           array         = item as Array;
                List <XElement> arrayElements = new List <XElement>();
                foreach (var element in array)
                {
                    arrayElements.Add(serializeCollectionElement(memberInfo, memberType.GetElementType(), element, arrayDimension));
                }
                return(arrayElements);
            }

            if (Type.GetTypeCode(memberType) == TypeCode.Object)
            {
                IValueXmlSerializer serializer = GetSerializerForType(memberType);
                if (serializer == null)
                {
                    return(item.ToString());
                }
                return(serializer.SerializeItem(item));
            }

            return(item.ToString());
        }
예제 #2
0
        private object serializeValue(MemberInfo memberInfo, Type memberType, object item, object parentItem, int arrayDimension = 0)
        {
            if (item == null)
            {
                return(string.Empty);
            }

            ISystemObject itemAsSystemObject = item as ISystemObject;

            if ((itemAsSystemObject != null) && (memberInfo.GetCustomAttribute <PersistDetailedAttribute>() == null))
            {
                return(itemAsSystemObject.GlobalID);
            }

            if (memberType.IsArray && (item is Array))
            {
                Array           array         = item as Array;
                List <XElement> arrayElements = new List <XElement>();
                foreach (var element in array)
                {
                    arrayElements.Add(serializeCollectionElement(memberInfo, memberType.GetElementType(), element, parentItem, arrayDimension));
                }
                return(arrayElements);
            }

            bool isCollection = false;
            Type elementType  = null;

            foreach (Type interfaceType in memberType.GetInterfaces())
            {
                if (interfaceType.IsGenericType && (interfaceType.GetGenericTypeDefinition() == typeof(ICollection <>)))
                {
                    isCollection = true;
                    elementType  = interfaceType.GetGenericArguments()[0];
                    break;
                }
            }
            if (isCollection)
            {
                IEnumerable     enumerable    = item as IEnumerable;
                List <XElement> arrayElements = new List <XElement>();
                foreach (var element in enumerable)
                {
                    arrayElements.Add(serializeCollectionElement(memberInfo, elementType, element, parentItem, arrayDimension));
                }
                return(arrayElements);
            }

            if (Type.GetTypeCode(memberType) == TypeCode.Object)
            {
                Type itemType        = item.GetType();
                Type serializeAsType = memberType;

                PersistSubclassAttribute persistSubclassAttribute = memberInfo.GetCustomAttributes <PersistSubclassAttribute>().FirstOrDefault();
                if (persistSubclassAttribute != null) // should check if given type is subclass of member type
                {
                    serializeAsType = persistSubclassAttribute.SubclassType;
                }

                PolymorphFieldAttribute   polymorphFieldAttribute = memberInfo.GetCustomAttributes <PolymorphFieldAttribute>().FirstOrDefault();
                Dictionary <Type, string> typeStringDictionary    = null;
                string itemTypeString = null;
                if (polymorphFieldAttribute != null)
                {
                    MethodInfo typeStringDictionaryGetterMethodInfo = parentItem.GetType().GetMethod(polymorphFieldAttribute.TypeStringDictionaryGetterName, memberLookupBindingFlags);
                    typeStringDictionary = typeStringDictionaryGetterMethodInfo.Invoke(parentItem, new object[] { }) as Dictionary <Type, string>;
                    if (typeStringDictionary?.TryGetValue(itemType, out itemTypeString) == true)
                    {
                        serializeAsType = itemType;
                    }
                }

                IValueXmlSerializer serializer = GetSerializerForType(serializeAsType);
                if (serializer == null)
                {
                    return(item.ToString());
                }
                XElement serializedItem = serializer.SerializeItem(item, parentItem);
                if ((polymorphFieldAttribute?.TypeAttributeName != null) && (itemTypeString != null))
                {
                    serializedItem.SetAttributeValue(polymorphFieldAttribute.TypeAttributeName, itemTypeString);
                }
                return(serializedItem);
            }

            return(item.ToString());
        }