public static void Main() { //Creates a new TestSimpleObject object. TestSimpleObject obj = new TestSimpleObject(); Console.WriteLine("Before serialization the object contains: "); obj.Print(); //Opens a file and serializes the object into it in binary format. Stream stream = File.Open("data.xml", FileMode.Create); SoapFormatter formatter = new SoapFormatter(); //BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); stream.Close(); //Empties obj. obj = null; //Opens file "data.xml" and deserializes the object from it. stream = File.Open("data.xml", FileMode.Open); formatter = new SoapFormatter(); //formatter = new BinaryFormatter(); obj = (TestSimpleObject)formatter.Deserialize(stream); stream.Close(); Console.WriteLine(""); Console.WriteLine("After deserialization the object contains: "); obj.Print(); Console.ReadLine(); }
private static int Main() { #if !MULTIMODULE_BUILD TestLdstr.Run(); TestException.Run(); TestThreadStaticNotInitialized.Run(); TestUntouchedThreadStaticInitialized.Run(); TestPointers.Run(); TestConstants.Run(); TestArray.Run(); TestMdArray.Run(); TestSimpleObject.Run(); TestFinalizableObject.Run(); TestStoreIntoOtherStatic.Run(); TestCctorCycle.Run(); TestReferenceTypeAllocation.Run(); TestReferenceTypeWithGCPointerAllocation.Run(); TestRelationalOperators.Run(); TestTryFinally.Run(); TestTryCatch.Run(); TestBadClass.Run(); TestRefs.Run(); TestDelegate.Run(); TestInitFromOtherClass.Run(); TestInitFromOtherClassDouble.Run(); TestDelegateToOtherClass.Run(); #else Console.WriteLine("Preinitialization is disabled in multimodule builds for now. Skipping test."); #endif return(100); }
public static void Run() { TestSimpleObject obj = new TestSimpleObject(); Console.WriteLine("Before serialization the object contains: "); obj.Print(); Stream stream = File.Open("data.xml", FileMode.Create); SoapFormatter formatter = new SoapFormatter(); formatter.Serialize(stream, obj); stream.Close(); obj = null; stream = File.Open("data.xml", FileMode.Open); formatter = new SoapFormatter(); obj = (TestSimpleObject)formatter.Deserialize(stream); stream.Close(); Console.WriteLine(""); Console.WriteLine("After deserialization the object contains: "); obj.Print(); }
public static void Main() { // Creates a new TestSimpleObject object. TestSimpleObject obj = new TestSimpleObject(); // Opens a file and serializes the object into it in binary format. Stream stream = File.Open("data.xml", FileMode.Create); SoapFormatter formatter = new SoapFormatter(); formatter.Serialize(stream, obj); stream.Close(); }
public static void Main() { // Create a new TestSimpleObject object. TestSimpleObject obj = new TestSimpleObject(); Console.WriteLine("\n Before serialization the object contains: "); obj.Print(); // Open a file and serialize the object into binary format. Stream stream = File.Open("DataFile.dat", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(stream, obj); // Print the object again to see the effect of the //OnSerializedAttribute. Console.WriteLine("\n After serialization the object contains: "); obj.Print(); // Set the original variable to null. obj = null; stream.Close(); // Open the file "DataFile.dat" and deserialize the object from it. stream = File.Open("DataFile.dat", FileMode.Open); // Deserialize the object from the data file. obj = (TestSimpleObject)formatter.Deserialize(stream); Console.WriteLine("\n After deserialization the object contains: "); obj.Print(); Console.ReadLine(); } catch (SerializationException se) { Console.WriteLine("Failed to serialize. Reason: " + se.Message); throw; } catch (Exception exc) { Console.WriteLine("An exception occurred. Reason: " + exc.Message); throw; } finally { stream.Close(); obj = null; formatter = null; } }
public static void Main() { // 三种序列化对比:Binary模式不生成xml文件, // soap模式保存成特殊协议的xml文件,支持不序列化nonserialize标签的属性 // xml模式不支持不序列化nonserialize标签的属性 // //Creates a new TestSimpleObject object. TestSimpleObject obj = new TestSimpleObject(); Console.WriteLine("Before serialization the object contains: "); obj.Print(); //Opens a file and serializes the object into it in binary format. //Stream stream = File.Open("data_xml.xml", FileMode.Create); Stream stream = File.Open("data_soap.xml", FileMode.Create); SoapFormatter formatter = new SoapFormatter(); //BinaryFormatter formatter = new BinaryFormatter(); //XmlSerializer formatter = new XmlSerializer(typeof(TestSimpleObject)); formatter.Serialize(stream, obj); stream.Close(); //Empties obj. obj = null; //Opens file "data.xml" and deserializes the object from it. //stream = File.Open("data_xml.xml", FileMode.Open); stream = File.Open("data_soap.xml", FileMode.Open); formatter = new SoapFormatter(); //formatter = new BinaryFormatter(); //formatter = new XmlSerializer(typeof(TestSimpleObject)); obj = (TestSimpleObject)formatter.Deserialize(stream); stream.Close(); Console.WriteLine(""); Console.WriteLine("After deserialization the object contains: "); obj.Print(); Console.Read(); }
/// <summary> /// Method for creating serialized collection by using SOAP format /// </summary> /// <returns></returns> public Stream CreateSerializedCollection() { //Creates a new TestSimpleObject object. TestSimpleObject obj = new TestSimpleObject(); Console.WriteLine("Before serialization the object contains: "); obj.Print(); //Opens a file and serializes the object into it in binary format. Stream stream = File.Open("data.xml", FileMode.Create); SoapFormatter formatter = new SoapFormatter(); //BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); stream.Close(); return(stream); }
public void InstantiatesBasicAttributesOnModel() { var store = new MemoryStore(); store.SetValue("name", "Foo"); store.SetValue("number", 5); store.SetValue("float", 37.5f); store.SetValue("list", "one, two, three, four"); store.SetValue("dice", "1d6+4"); var obj = new TestSimpleObject(); var test = store.Deserialize <TestSimpleObject>(obj); Assert.Equal(test.Name, "Foo"); Assert.Equal(test.Number, 5); Assert.Equal(test.FloatNumber, 37.5f); Assert.NotStrictEqual(test.ListOfValues, new string[] { "one", "two", "three", "four" }); Assert.Equal(test.Optional, ""); Assert.Equal("defaultString", test.OptionalWithDefault); Assert.Equal("1d6+4", test.DiceValues.ToString()); }
private static int Main() { #if !MULTIMODULE_BUILD TestLdstr.Run(); TestException.Run(); TestThreadStaticNotInitialized.Run(); TestUntouchedThreadStaticInitialized.Run(); TestPointers.Run(); TestConstants.Run(); TestArray.Run(); TestArrayOutOfRange.Run(); TestMdArray.Run(); TestSimpleObject.Run(); TestFinalizableObject.Run(); TestStoreIntoOtherStatic.Run(); TestCctorCycle.Run(); TestReferenceTypeAllocation.Run(); TestReferenceTypeWithGCPointerAllocation.Run(); TestReferenceTypeWithReadonlyNullGCPointerAllocation.Run(); TestRelationalOperators.Run(); TestTryFinally.Run(); TestTryCatch.Run(); TestBadClass.Run(); TestRefs.Run(); TestDelegate.Run(); TestInitFromOtherClass.Run(); TestInitFromOtherClassDouble.Run(); TestDelegateToOtherClass.Run(); TestLotsOfBackwardsBranches.Run(); TestDrawCircle.Run(); TestValueTypeDup.Run(); TestFunctionPointers.Run(); TestGCInteraction.Run(); TestDuplicatedFields.Run(); TestInstanceDelegate.Run(); #else Console.WriteLine("Preinitialization is disabled in multimodule builds for now. Skipping test."); #endif return(100); }
void Start() { //Notice how you pass no parameter into this //extension method even though you had one in the //method declaration. The transform object that //this method is called from automatically gets //passed in as the first parameter print (2.828793e+00); //Creates a new TestSimpleObject object. TestSimpleObject obj = new TestSimpleObject(); print("Before serialization the object contains: "); obj.Print(); //Opens a file and serializes the object into it in binary format. Stream stream = File.Open("data.xml", FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); //BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); stream.Close(); //Empties obj. obj = null; //Opens file "data.xml" and deserializes the object from it. stream = File.Open("data.xml", FileMode.Open); formatter = new BinaryFormatter(); //formatter = new BinaryFormatter(); obj = (TestSimpleObject)formatter.Deserialize(stream); stream.Close(); print("After deserialization the object contains: "); obj.Print(); // Console.WriteLine("you suck"); }
/// <summary> /// Method for deserializing collections in SOAP format /// </summary> /// <param name="stream">serialized object in binary format that we want to deserialize</param> public void DeserializeCollection(Stream stream) { TestSimpleObject obj = new TestSimpleObject(); //Empties obj. obj = null; //Opens file "data.xml" and deserializes the object from it. stream = File.Open("data.xml", FileMode.Open); SoapFormatter formatter = new SoapFormatter(); formatter = new SoapFormatter(); //formatter = new BinaryFormatter(); obj = (TestSimpleObject)formatter.Deserialize(stream); stream.Close(); Console.WriteLine(""); Console.WriteLine("After deserialization the object contains: "); obj.Print(); }
public void SerializeTests() { var yamlStore = new YamlObjectStore(); var obj = new TestSimpleObject(); obj.Name = "Foo"; obj.Number = 39; obj.FloatNumber = 2019.24f; obj.ListOfValues = new string[] { "one", "two", "three" }; obj.Optional = "Optional"; obj.DiceValues = new SilverNeedle.Dice.Cup(SilverNeedle.Dice.Die.D6()); obj.IgnoreMe = "ignore"; yamlStore.Serialize(obj); Assert.Equal("Tests.Serialization.ObjectStoreSerializerTests+TestSimpleObject", yamlStore.GetString("serialized-type")); Assert.Equal("Foo", yamlStore.GetString("name")); Assert.Equal(39, yamlStore.GetInteger("number")); Assert.Equal(2019.24f, yamlStore.GetFloat("float")); Assert.Equal(new string[] { "one", "two", "three" }, yamlStore.GetList("list")); Assert.Equal("Optional", yamlStore.GetString("optional")); Assert.False(yamlStore.HasKey("IgnoreMe")); }