Exemplo n.º 1
0
 // Insertion sort
 internal static void InsertSortedMetaData(List <ReflectedMemberInfo> list, ReflectedMemberInfo minfo)
 {
     for (int i = 0; i < list.Count; i++)
     {
         if (list[i].Name.CompareTo(minfo.Name) > 0)
         {
             list.Insert(i, minfo);
             return;
         }
     }
     list.Add(minfo);
 }
Exemplo n.º 2
0
        // Deserializes classes.
        private object DeserializeComplexType(Type objectType, int cacheID)
        {
            // Complex object
            ObjectMetaData metaData = GetMetaData(objectType);

            if (metaData.DynamicSerializer != null)
            {
                // use compiled deserializer
                object newObject = metaData.DynamicSerializer.Deserialize(this, cacheID);
                return(newObject);
            }

            if (metaData.IsIAltSerializable)
            {
                // Read complex object...
                object ialtobj = Activator.CreateInstance(objectType);
                if (cacheID > 0)
                {
                    Cache.SetCachedObjectId(ialtobj, cacheID);
                }
                // Call this object as an IAltSerializable and move on
                ((IAltSerializable)ialtobj).Deserialize(this);
                return(ialtobj);
            }

            if (metaData.ImplementsIList)
            {
                // Implements IList, use special definition for this method
                return(DeserializeList(objectType, cacheID, metaData));
            }
            else if (metaData.ImplementsIDictionary)
            {
                // Implements IDictionary; use special definition for this method ..
                object dict = DeserializeDictionary(objectType, cacheID);

                // Reflect on each field, deserializing the data
                foreach (ReflectedMemberInfo info in metaData.Values)
                {
                    if (SerializeProperties == false && info.FieldType.IsSerializable == false)
                    {
                        // When deserializing fields, skip the 'nonserializable' ones.
                        continue;
                    }

                    object deserializedObject = DeserializeElement(info.FieldType);
                    info.SetValue(dict, deserializedObject);
                }
                return(dict);
            }

            if (metaData.IsISerializable)
            {
                // Implements ISerializabe; use that interface for serialization/deserialization
                Dictionary <string, object> sinfo;

                SerializationInfo info    = new SerializationInfo(objectType, new AltFormatter());
                StreamingContext  context = new StreamingContext(StreamingContextStates.All);
                sinfo = (Dictionary <string, object>)Deserialize(typeof(Dictionary <string, object>));
                foreach (KeyValuePair <string, object> kvp in sinfo)
                {
                    info.AddValue(kvp.Key, kvp.Value);
                }

                ConstructorInfo construct = (ConstructorInfo)metaData.Extra;
                return(construct.Invoke(new object[] { info, context }));
            }

            if (metaData.IsGenericList)
            {
                FieldInfo itemsField  = (FieldInfo)metaData.Extra;
                Type      arrayType   = itemsField.FieldType;
                object    arrayObject = DeserializeArray(arrayType, 0);
                //object newList = Activator.CreateInstance(objectType, new object[] { ((Array)arrayObject).Length });
                object newList = Activator.CreateInstance(objectType, new object[] { arrayObject });
                if (cacheID > 0)
                {
                    Cache.SetCachedObjectId(newList, cacheID);
                }
                return(newList);
            }

            object obj = Activator.CreateInstance(objectType);

            // This object might be referenced by other objects
            // that it loads, so we need to cache it first!
            if (cacheID > 0)
            {
                Cache.SetCachedObjectId(obj, cacheID);
            }


            if (SerializePropertyNames)
            {
                // Deserialize each stored property
                int propertyCount = (int)((short)Deserialize(typeof(short)));
                for (int i = 0; i < propertyCount; i++)
                {
                    string propertyName      = ReadString();
                    ReflectedMemberInfo info = metaData.FindMemberInfoByName(propertyName);
                    if (info == null)
                    {
                        // Ignore the field not found.
                        //throw new AltSerializeException("Unable to find the property '" + propertyName + "' in object type '" + objectType.FullName + "'.");
                    }
                    else
                    {
                        // Deserialize the object into the property
                        object desobj = DeserializeElement(info.FieldType);
                        info.SetValue(obj, desobj);
                    }
                }
            }
            else
            {
                // Reflect on each field, deserializing the data
                foreach (ReflectedMemberInfo info in metaData.Values)
                {
                    if (SerializeProperties == false && info.FieldType.IsSerializable == false)
                    {
                        // When deserializing fields, skip the 'nonserializable' ones.
                        continue;
                    }

                    object deserializedObject = DeserializeElement(info.FieldType);
                    info.SetValue(obj, deserializedObject);
                }
            }

            return(obj);
        }