சி ஷார்ப்/தொடராக்கல்
ஓர் பொருளை எப்படி தொடராக்குவது?
தொகுusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace How_to_Serialize_an_Object
{
class Program
{
static void Main(string[] args)
{
// C#
string data = "This must be stored in a file.";
// Create file to save the data to
FileStream fs = new FileStream("SerializedString.Data", FileMode.Create);
// Create a BinaryFormatter object to perform the serialization
BinaryFormatter bf = new BinaryFormatter();
// Use the BinaryFormatter object to serialize the data to the file
bf.Serialize(fs, data);
// Close the file
fs.Close();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Date_Time_serialization
{
class Program
{
static void Main(string[] args)
{
// C#
// Create file to save the data to
FileStream fs = new FileStream("SerializedDate.Data", FileMode.Create);
// Create a BinaryFormatter object to perform the serialization
BinaryFormatter bf = new BinaryFormatter();
// Use the BinaryFormatter object to serialize the data to the file
bf.Serialize(fs, System.DateTime.Now);
// Close the file
fs.Close();
}
}
}
தொடராக்கியதை எப்படி மீண்டும் பொருளாக்குவது
தொகுusing System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace How_to_Serialize_an_Object
{
class Program
{
static void Main(string[] args)
{
//// C#
//string data = "This must be stored in a file.";
//// Create file to save the data to
//FileStream fs = new FileStream("SerializedString.Data", FileMode.Create);
//// Create a BinaryFormatter object to perform the serialization
//BinaryFormatter bf = new BinaryFormatter();
//// Use the BinaryFormatter object to serialize the data to the file
//bf.Serialize(fs, data);
//// Close the file
//fs.Close();
//DeSerialize
// C#
// Open file from which to read the data
FileStream fs = new FileStream("SerializedString.Data", FileMode.Open);
// Create a BinaryFormatter object to perform the deserialization
BinaryFormatter bf = new BinaryFormatter();
// Create the object to store the deserialized data
string data = "";
// Use the BinaryFormatter object to deserialize the data from the file
data = (string)bf.Deserialize(fs);
// Close the file
fs.Close();
// Display the deserialized string
Console.WriteLine(data);
Console.ReadLine();
}
}
}
பிரிவிகளை எப்படித் தொடராக்குவது
தொகுHow to Create Classes That Can Be Serialized
// C#
[Seriali
:zable]
class ShoppingCartItem
{
public int productId;
public decimal price;
public int quantity;
[NonSerialized]
public decimal total;
public ShoppingCartItem(int _productID, decimal _price, int _quantity)
{
productId = _productID;
price = _price;
quantity = _quantity;
total = price * quantity;
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace How_to_Create_Classes_That_Can_Be_Serialized
{
// C#
[Serializable]
class ShoppingCartItem : IDeserializationCallback
{
public int productId;
public decimal price;
public int quantity;
[NonSerialized]
public decimal total;
public ShoppingCartItem(int _productID, decimal _price, int _quantity)
{
productId = _productID;
price = _price;
quantity = _quantity;
total = price * quantity;
}
void IDeserializationCallback.OnDeserialization(Object sender)
{
// After deserialization, calculate the total
total = price * quantity;
}
}
}