예제 #1
0
        public static void SerializeItem()
        {
            IFormatterConverter converter     = new FormatterConverter();
            IFormatter          formatter     = new BinaryFormatter();
            SeDeClass           seDeClass     = new SeDeClass();
            SeDeClass           t             = new SeDeClass();
            SeDeTestClass       seDeTestClass = new SeDeTestClass();


            StreamingContext context    = new StreamingContext(StreamingContextStates.All, "foo");
            IFormatter       formatter1 = new BinaryFormatter(null, context);

            seDeTestClass._id = 0;

            seDeTestClass.MyValue = "Just do a Test";
            seDeClass._id         = 0;
            seDeClass.MyValue     = "Just do a Test";

            FileStream fileStream1 = new FileStream("Test1.txt", FileMode.Create);
            FileStream fileStream2 = new FileStream("Test2.txt", FileMode.Create);
            FileStream fileStream3 = new FileStream("Test3.txt", FileMode.Create);

            formatter.Serialize(fileStream1, seDeClass);
            formatter.Serialize(fileStream2, seDeTestClass);
            formatter1.Serialize(fileStream3, seDeClass);

            fileStream1.Close();
            fileStream2.Close();
            fileStream3.Close();
        }
예제 #2
0
        public static void DeserializeItem()
        {
            IFormatter       formatter  = new BinaryFormatter();
            StreamingContext context    = new StreamingContext(StreamingContextStates.File, "foo");
            IFormatter       formatter1 = new BinaryFormatter(null, context);

            FileStream fileStream1 = new FileStream("Test1.txt", FileMode.Open);
            FileStream fileStream2 = new FileStream("Test2.txt", FileMode.Open);
            FileStream fileStream3 = new FileStream("Test3.txt", FileMode.Open);

            SeDeClass     seDeClass     = (SeDeClass)formatter.Deserialize(fileStream1);
            SeDeTestClass seDeTestClass = (SeDeTestClass)formatter.Deserialize(fileStream2);
            SeDeClass     seDeClass2    = (SeDeClass)formatter1.Deserialize(fileStream3);

            Console.WriteLine($"for Test1 ,Value is:{seDeClass.MyValue}, Id is :{seDeClass._id}, Test Method value is:{ seDeClass._testmethodvalue}");
            Console.WriteLine($"for Test2 ,Value is:{seDeTestClass.MyValue}, Id is :{seDeTestClass._id}");
            Console.WriteLine($"for Test3 ,Value is:{seDeClass2.MyValue}, Id is :{seDeClass2._id}, Test Method value is:{ seDeClass2._testmethodvalue}");
        }