تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
الدرس الستون : Object Serialization
#1
[COLOR="#006400"]كاتب المقال وجميع الردود
احمد جمال
[/COLOR]


بسم الله الرحمن الرحيم .
السلام عليكم ورحمة الله وبركاته .

سنتعرف اليوم سريعاً عن مفهوم ال Object Serialization .

عمل الفئة الخاصة بك لتكون Serializable :

كل ما في الأمر أن تضع الكلمة : [Serializable] اعلى اسم الكلاس بالشكل التالي مثلاً :
C#:
رمز برمجي:
كود :
[Serializable]
public class serial
{
}
vb.net:
كود :
<Serializable()> _
Public Class serial
    
End Class

ماذا استفيد من كون الفئة الخاصة بي Serializable ؟

معظم الفئات الاساسية تتيح لك كونها Serializable للاستفادة من بعض الخصائص مثل الكتابة المباشرة إلى ملفات القرص الصلب كما تعلمنا سابقاً ، هذا المثال :
C#:

كود :
serial sample =new serial();
using(Stream fs1 = new FileStream("data.txt",
FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fw1, sample );
}
vb.net:

كود :
Dim sample As New serial()
Using fs1 As Stream = New FileStream("data.txt", FileMode.Create, FileAccess.Write, FileShare.None)
    binFormat.Serialize(fw1, sample)
End Using

ماذا أيضاً ؟
هناك عدد كبير من الكائنات التي قد لا تقبل التعامل مع الفئات الخاصة بك إلا لو كانت Serializable ، منها ال View State في صفحات ال ASP.net .

هناك العديد من الطرق لعمل Serialization لكائناتك ، منها BinaryFormatter :
C#:
كود :
Dim binFormatter As New BinaryFormatter()
Using fs1 As Stream = New FileStream("data.txt", FileMode.Create, FileAccess.Write, FileShare.None)
    binFormat.Serialize(fs1, myobject)
End Using

vb.net:
كود :
BinaryFormatter binFormatter = new BinaryFormatter();
using(Stream fs1 = new FileStream("data.txt",
FileMode.Create, FileAccess.Write, FileShare.None))
{
binFormat.Serialize(fs1, myobject);
}

ولعمل Deserializing :
C#:
كود :
BinaryFormatter binFormatter = new BinaryFormatter();
using(Stream fs1 = File.OpenRead("data.txt"))
{
newobject car =
(objectclass)binFormatter.Deserialize(fs1);
}


vb.net:

كود :
Dim binFormatter As New BinaryFormatter()
Using fs1 As Stream = File.OpenRead("data.txt")
    Dim car As newobject = DirectCast(binFormatter.Deserialize(fs1), objectclass)
End Using
الرد }}}
تم الشكر بواسطة:


الردود في هذا الموضوع
الدرس الستون : Object Serialization - بواسطة oneyemenweb2_mybb_import4801 - 14-10-12, 05:49 PM


التنقل السريع :


يقوم بقرائة الموضوع: