Exemplo n.º 1
0
        public override object Deserialize(Stream serializationStream)
        {
            List <Object> returnObjects = new List <object>();

            readObjectsList = new List <string>();
            if (serializationStream != null)
            {
                string readData;
                using (StreamReader reader = new StreamReader(serializationStream, Encoding.UTF8, false, 32, true))
                {
                    // Wczytuję wszystkie dane
                    readData = reader.ReadToEnd();
                    //Dzielę na zbiory właściwości poszczegolnych obiektów
                    string[] objectsData = readData.Split(';');
                    foreach (string objectData in objectsData)
                    {
                        readObjectsList.Add(objectData);
                    }
                }

                foreach (string data in readObjectsList)
                {
                    //Rozdzielam na poszczególne właściwości obiektu
                    String[] objectStrings = data.Split('\n');
                    if (objectStrings[0] == "")
                    {
                        List <String> buff = objectStrings.OfType <String>().ToList();
                        buff.RemoveAt(0);
                        objectStrings = buff.ToArray();
                    }

                    //Rozdzielam na informacje o poszczególnej właściwości
                    string[] objAtr = objectStrings[0].Split('|');
                    if (objAtr.Length != 3)
                    {
                        continue;
                    }

                    //Zapisuję do Słownika obiekt pod referencją do niego
                    ReferencesDictionary.Add(objAtr[2],
                                             FormatterServices.GetSafeUninitializedObject(customBinder.BindToType(objAtr[0], objAtr[1])));
                }

                foreach (string data in readObjectsList)
                {
                    //Rozdzielam na poszczególne właściwości obiektu
                    String[] objectStrings = data.Split('\n');
                    if (objectStrings[0] == "")
                    {
                        List <String> buff = objectStrings.OfType <String>().ToList();
                        buff.RemoveAt(0);
                        objectStrings = buff.ToArray();
                    }

                    //Rozdzielam na informacje o poszczególnej właściwości
                    string[] objAtr = objectStrings[0].Split('|');
                    if (objAtr.Length != 3)
                    {
                        continue;
                    }

                    // Tworzę obiekt przechowujący typ danego obiektu
                    Type deserializedObjType = customBinder.BindToType(objAtr[0], objAtr[1]);
                    // Tworzę nowe SerializationInfo i context
                    SerializationInfo info     = new SerializationInfo(deserializedObjType, new FormatterConverter());
                    StreamingContext  _context = new StreamingContext(StreamingContextStates.File);

                    // Wczytuję informacje do SerializationInfo
                    for (int i = 1; i < objectStrings.Length; i++)
                    {
                        string[] objAtrs = objectStrings[i].Split('|');
                        //zwracam typ danego atrybutu obiektu
                        Type atrType = customBinder.BindToType(objAtr[0], objAtrs[0]);
                        // sprawdzam czy typ obiektu nie jest nullem
                        if (atrType == null)
                        {
                            //sprawdzam czy powinien być nullem
                            if (!objAtrs[0].Equals("null"))
                            {
                                SaveValueToSerialInfo(info, Type.GetType(objAtrs[0]), objAtrs[1], objAtrs[2]);
                            }
                            else
                            {
                                info.AddValue(objAtrs[1], null);
                            }
                        }
                        // jak tu doszło to jest referencją
                        else
                        {
                            info.AddValue(objAtrs[1], ReferencesDictionary[objAtrs[2]], atrType);
                        }
                    }

                    // Tworzę listę typów parametrów konstruktora obiektu
                    Type[] constructorTypes = { info.GetType(), _context.GetType() };
                    // tworzę listę argumentów konstruktora
                    object[] constructorArguments = { info, _context };
                    // tworzę obiekt
                    ReferencesDictionary[objAtr[2]].GetType().GetConstructor(constructorTypes)
                    .Invoke(ReferencesDictionary[objAtr[2]], constructorArguments);
                    returnObjects.Add(ReferencesDictionary[objAtr[2]]);
                }
            }

            return(returnObjects[0]);
        }
Exemplo n.º 2
0
        public override object Deserialize(Stream serializationStream)
        {
            List <string> ObjectsInString = new List <string>();
            List <Object> Objects         = new List <object>();

            if (serializationStream != null)
            {
                using (StreamReader reader = new StreamReader(serializationStream, Encoding.UTF8, false, 32, true))
                {
                    string   data = reader.ReadToEnd();
                    string[] objs = data.Split('$');
                    foreach (string obj in objs)
                    {
                        ObjectsInString.Add(obj);
                    }
                }

                foreach (string obj in ObjectsInString)
                {
                    string[] objectProperties = obj.Split('\n');
                    if (objectProperties[0] == "")
                    {
                        List <String> tmp = objectProperties.OfType <String>().ToList();
                        tmp.RemoveAt(0);
                        objectProperties = tmp.ToArray();
                    }

                    string[] objectAtrribute = objectProperties[0].Split("->");
                    if (objectAtrribute.Length != 3)
                    {
                        continue;
                    }

                    RefToObjects.Add(
                        objectAtrribute[2], FormatterServices.GetSafeUninitializedObject(CustomBinder.BindToType(objectAtrribute[0], objectAtrribute[1])));
                }

                foreach (string obj in ObjectsInString)
                {
                    string[] objectProperties = obj.Split('\n');
                    if (objectProperties[0] == "")
                    {
                        List <String> tmp = objectProperties.OfType <String>().ToList();
                        tmp.RemoveAt(0);
                        objectProperties = tmp.ToArray();
                    }

                    string[] objectAtrribute = objectProperties[0].Split("->");
                    if (objectAtrribute.Length != 3)
                    {
                        continue;
                    }

                    Type objType = CustomBinder.BindToType(objectAtrribute[0], objectAtrribute[1]);
                    SerializationInfo serializationInfo = new SerializationInfo(objType, new FormatterConverter());
                    StreamingContext  streamingContext  = new StreamingContext(StreamingContextStates.File);

                    for (int i = 1; i < objectProperties.Length; i++)
                    {
                        string[] objectAttributes = objectProperties[i].Split("->");
                        Type     atrributeType    = CustomBinder.BindToType(objectAtrribute[0], objectAttributes[0]);
                        if (atrributeType == null)
                        {
                            DeSerializeUnknownType(serializationInfo, Type.GetType(objectAttributes[0]), objectAttributes[1], objectAttributes[2]);
                        }
                        else
                        {
                            serializationInfo.AddValue(objectAttributes[1], RefToObjects[objectAttributes[2]], atrributeType);
                        }
                    }

                    Type[]   constructorTypes     = { serializationInfo.GetType(), streamingContext.GetType() };
                    object[] constructorArguments = { serializationInfo, streamingContext };
                    RefToObjects[objectAtrribute[2]].GetType().GetConstructor(constructorTypes).Invoke(RefToObjects[objectAtrribute[2]], constructorArguments);
                    Objects.Add(RefToObjects[objectAtrribute[2]]);
                }
            }
            return(Objects[0]);
        }