Title: Serialization in .NET Framework
1(No Transcript)
2???????????? ?? ?????
???????????? ?? .NET Framework
http//www.nakov.com/dotnet-project/
?????? ??????? ????????? ???????, Sciant Inc.
www.sciant.com Microsoft Student
Consultant www.fmi.uni-sofia.bg/microsoft/
3?????????? ??????
- ?????? ???????? ?? .NET Framework ? CLR (Common
Language Runtime) - ?????? ???????? ?? ?????? ??????? ?? ?????? ?
.NET (Common Type System) - ?????? ???????? ?? ???????????? ?? ??????? ? .NET
(Framework Class Library) - ???????? ?? ????? C
- ???????? ?? ?????? ?? ?????
- ?????? ???????? ?? Reflection
- ???????? ?? Attributes
- ???????? ?? ?????? ? XML ? .NET Framework
4??????????
- ????????????
- XML ????????????
- ?????????? FormatterServices
5????????????
- ????????????
- ????? e ?????????????
- ?? ????? ?? ?????????
- ???? ?????????????
- ??? ?? ????????????
- ?????? ??????e?? (Formatters)
- ???????? ?? ????????????
- ????? ?????? ????????????
- ????? ?????? ??????????????
- IDeserializationCallback
- ISerializable
- ???????????? ?? ??????????????
- Streaming Contexts
- XML ????????????
6????? e ?????????????
- ???????????? (Serialization)
- ??????, ????? ??????????? ????? ??? ??????? ????
?? ?????? ?? ????? ?? ??????? - ??????? ???? ?? ???? ??????? (binary) ??? ???????
(XML) - ?????????????? (Deserialization)
- ??????, ????? ????? ????? ?? ??????? ??????? ??
?????? - ??????? ?????????????? ???????
- ???????????, ?????, ???????? ?? ??? ???????
7?? ????? ?? ?????????
- ????????? ?? ??????????? ?? ?????
- ???? ???????? ???? ?? ??????? ??????????? ?? ??
??????? (????, ??) - ????????? ?? ????? ???? ?????
- ??? ???????? ????? ?? ?? ???????? ?????
- .NET Framework ?? ???????? ????????
- ?? ????????? ?? session state-? ? ASP.NET
- ?? ???????? ? clipboard ?? Windows Forms
- ?? ????????? ?? ?????? ?? ???????? ?? ????
Application Domain ? ???? - ?? ??????? ???????? (Deep Copy)
- ? Remoting ????????????
8?? ????? ?? ????????? (2)
- ???? ???? ???? ????? ???? ????????? ? ????? ??
???????, ??? ???? ?? ???? - ?????????
- ???????????
- ?????? ???? ??????? ?? ?????????
- ?? ??????? ?? ?????????????? ?????
- ????? ?? ?? ???????????? ???????? ????? ??
??????, ?.?. ??????, ????? ?? ????????? ???? ??
????? - ?????????? ??????????? ????? ????? ???? ?? ??????
?? ????? ? ???????? ?????
9???? ?????????????
- ??????????? ?? ???? ????? ???? ?? ?? ??????? ?
?????, ??? ???????????? - ??????????? ??, ?????
- ?????? ?????????? ? 10 000 ?????
- ?????? ???? ?? ????????? ??????
- ??? ??? ????? ????, ????? ?? ?????? ? ?????????,
??????????? ?? ???????? ??? ??????/??????,
?????????? ?? ??????, ?????? ?????? ??? ?????
??????, ?????? ??? ?????????, ??????, - ??? ??-??????? ????????? ?? ???????? ????
- ?????????????? ????? ????? ???? ?????? ?????????
? ???????????
10??? ?? ????????????
String str "??? ?? ?? ????!" BinaryFormatter f
new BinaryFormatter() using (Stream s new
FileStream("sample.bin")) f.Serialize(s,
str)
- ???????, ????? ?? ?? ???????????
- ??????? ???? ?? ???? ???????? ??? Int32,
String, DateTime, Exception, Image, ArrayList,
Hashtable, ????????????? ????????? ????, - ?? ???? ??? ????????? ?????????? (???????? ??
??-?????)
11??? ?? ????????????
String str "??? ?? ?? ????!" BinaryFormatter f
new BinaryFormatter() using (Stream s new
FileStream("sample.bin")) f.Serialize(s,
str)
- ????????? ?? ???????? (formatter)
- ?????????? ? ????????? ????, ????? ????????????
IFormatter - ?????????? ???????? ?????? ?????? ??
??????????????? ? ????????????????? ?? ????????
(????) ?? ??????
12??? ?? ????????????
String str "??? ?? ?? ????!" BinaryFormatter f
new BinaryFormatter() using (Stream s new
FileStream("sample.bin")) f.Serialize(s,
str)
- ???????, ? ????? ?? ?? ???????????? ???????
- ???? ????????? ?? ?????? ????????? ?? ?????
System.IO.Stream - ???? ????????, ?? ??????? ???? ?? ?? ???????????
? MemoryStream, FileStream, NetworkStream,
13??? ?? ????????????
String str "??? ?? ?? ????!" BinaryFormatter f
new BinaryFormatter() using (Stream s new
FileStream("sample.bin")) f.Serialize(s,
str)
- ?????? ?????? ?? ????????????
- ?????????? ??????? (???? reflection) ??????
????-?????????? ?? ?????? - ?????????? ?? ????-???????????? ???? ????????
???????????? ?? ?????? - ???????????? ?? ???? ??????? ?? ???????????, ???
?????????? ???????
14?????? ????????? (Formatters)
- ??????????? ???????? ???????? ?? ??????????? ??
??????????????? ?????? - CLR ??????? ??????????? ?? ????-?????????? ? ????
reflection ??????? ??????????? ?? - ???? ????????? ?? ??????? ?? ?????????
- ????????? (formatter) o? System.Runtime.Serializat
ion - BinaryFormatter
- SoapFormatter
- ????????????? ??????????
- ?????????? Formatter ?????????? ????, ?????
?????? ??????????????
15???????? ?? ?????????????
16????? ?????? ????????????
using System.IO using System.Runtime.Serializatio
n.Formatters Serializable class
FirstExample public int Number
NonSerialized public int Id public string
Name class Serializer public void
Serialize() FirstExample obj new
FirstExample() BinaryFormatter f new
BinaryFormatter() using (Stream stream new
FileStream("x.bin"))
f.Serialize(stream, obj) public void
Deserialize() ...
17????? ?????? ????????????
using System.IO using System.Runtime.Serializatio
n.Formatters Serializable class
FirstExample public int Number
NonSerialized public int Id public string
Name class Serializer public void
Serialize() FirstExample obj new
FirstExample() BinaryFormatter f new
BinaryFormatter() using (Stream stream new
FileStream("x.bin"))
f.Serialize(stream, obj) public void
Deserialize() ...
18????? ?????? ????????????
using System.IO using System.Runtime.Serializatio
n.Formatters Serializable class
FirstExample public int Number
NonSerialized public int Id public string
Name class Serializer public void
Serialize() FirstExample obj new
FirstExample() BinaryFormatter f new
BinaryFormatter() using (Stream stream new
FileStream("x.bin"))
f.Serialize(stream, obj) public void
Deserialize() ...
19????? ?????? ????????????
using System.IO using System.Runtime.Serializatio
n.Formatters Serializable class
FirstExample public int Number
NonSerialized public int Id public string
Name class Serializer public void
Serialize() FirstExample obj new
FirstExample() BinaryFormatter f new
BinaryFormatter() using (Stream stream new
FileStream("x.bin"))
f.Serialize(stream, obj) public void
Deserialize() ...
20????? ?????? ????????????
using System.IO using System.Runtime.Serializatio
n.Formatters Serializable class
FirstExample public int Number
NonSerialized public int Id public string
Name class Serializer public void
Serialize() FirstExample obj new
FirstExample() BinaryFormatter f new
BinaryFormatter() using (Stream stream new
FileStream("x.bin"))
f.Serialize(stream, obj) public void
Deserialize() ...
21????? ?????? ????????????
using System.IO using System.Runtime.Serializatio
n.Formatters Serializable class
FirstExample public int Number
NonSerialized public int Id public string
Name class Serializer public void
Serialize() FirstExample obj new
FirstExample() BinaryFormatter f new
BinaryFormatter() using (Stream stream new
FileStream("x.bin"))
f.Serialize(stream, obj) public void
Deserialize() ...
22????? ?????? ????????????
using System.IO using System.Runtime.Serializatio
n.Formatters Serializable class
FirstExample public int Number
NonSerialized public int Id public string
Name class Serializer public void
Serialize() FirstExample obj new
FirstExample() BinaryFormatter f new
BinaryFormatter() using (Stream stream new
FileStream("x.bin"))
f.Serialize(stream, obj) public void
Deserialize() ...
23????? ?????? ????????????
using System.IO using System.Runtime.Serializatio
n.Formatters Serializable class
FirstExample public int Number
NonSerialized public int Id public string
Name class Serializer public void
Serialize() FirstExample obj new
FirstExample() BinaryFormatter f new
BinaryFormatter() using (Stream stream new
FileStream("x.bin"))
f.Serialize(stream, obj) public void
Deserialize() ...
24????? ?????? ????????????
using System.IO using System.Runtime.Serializatio
n.Formatters Serializable class
FirstExample public int Number
NonSerialized public int Id public string
Name class Serializer public void
Serialize() FirstExample obj new
FirstExample() BinaryFormatter f new
BinaryFormatter() using (Stream stream new
FileStream("x.bin"))
f.Serialize(stream, obj) public void
Deserialize() ...
25????? ?????? ??????????????
- using System.IO
- using System.Runtime.Serialization.Formatters
- Serializable
- class FirstExample
-
- public int Number
- NonSerialized public int Id
- public string Name
-
- class Serializer
-
- public void Serialize() ...
- public void Deserialize()
-
- BinaryFormatter f new BinaryFormatter()
- using (Stream stream new FileStream("x.bin")
) -
- FirstExample fe (FirstExample)
- f.Deserialize(stream)
26????? ?????? ??????????????
- using System.IO
- using System.Runtime.Serialization.Formatters
- Serializable
- class FirstExample
-
- public int Number
- NonSerialized public int Id
- public string Name
-
- class Serializer
-
- public void Serialize() ...
- public void Deserialize()
-
- BinaryFormatter f new BinaryFormatter()
- using (Stream stream new FileStream("x.bin")
) -
- FirstExample fe (FirstExample)
- f.Deserialize(stream)
27????? ?????? ??????????????
- using System.IO
- using System.Runtime.Serialization.Formatters
- Serializable
- class FirstExample
-
- public int Number
- NonSerialized public int Id
- public string Name
-
- class Serializer
-
- public void Serialize() ...
- public void Deserialize()
-
- BinaryFormatter f new BinaryFormatter()
- using (Stream stream new FileStream("x.bin")
) -
- FirstExample fe (FirstExample)
- f.Deserialize(stream)
28???????????? 1
29???????????? 2
30???????????? 3
- ??????? ???????? ?? ??????
31IDeserializationCallback
- ??? ??????, ? ????? ?? ??????, ???? ???? ???????
? ??????????????, ?? ?? ???????? ?????
???????????? ???? - ???????????? ????????? IDeserializationCallback ?
???????????? ?????? OnDeserialization() - CLR ????????? ?????? ???? ??????? ??????????????
?? ?????? - ??????? ?, ?? ? ??????? ?? ?????????? ?? ??????
?????? ????-?????????? ?? ???? ???????????????
32???????????? 4
33ISerializable
- ????????? ????????????? ?? ?????????????
???????????? - .ctor(SerializationInfo info, StreamingContext
context) - ?? ?????????? base(info, context)
- ISerializable.GetObjectData( SerializationInfo
info, StreamingContext context) - ?? ?????????? base.GetObjectData(info, context)
- ????????? ?? ?????????? ????????????, ??????
???????????? ???? ??? ?? ?? ??????? ?? ?? ????????
34???????????? ?? ??????????????
using System.Runtime.Serialization Serializable
class Person ISerializable private string
mName private int mAge private
Person(SerializationInfo aInfo,
StreamingContext aContext) mName
(string)?Info.GetString("Person's name")
mAge aInfo.GetInt32("Person's age")
void ISerializable.GetObjectData(SerializationInfo
aInfo, StreamingContext aContext)
aInfo.AddValue("Person's name", mName)
aInfo.AddValue("Person's age", mAge)
35???????????? ?? ??????????????
using System.Runtime.Serialization Serializable
class Person ISerializable private string
mName private int mAge private
Person(SerializationInfo aInfo,
StreamingContext aContext) mName
(string)aInfo.GetString("Person's name")
mAge aInfo.GetInt32("Person's age")
void ISerializable.GetObjectData(SerializationInfo
aInfo, StreamingContext aContext)
aInfo.AddValue("Person's name", mName)
aInfo.AddValue("Person's age", mAge)
36???????????? ?? ??????????????
using System.Runtime.Serialization Serializable
class Person ISerializable private string
mName private int mAge private
Person(SerializationInfo aInfo,
StreamingContext aContext) mName
(string)?Info.GetString("Person's name")
mAge aInfo.GetInt32("Person's age")
void ISerializable.GetObjectData(SerializationInfo
aInfo, StreamingContext aContext)
aInfo.AddValue("Person's name", mName)
aInfo.AddValue("Person's age", mAge)
37???????????? ?? ??????????????
using System.Runtime.Serialization Serializable
class Person ISerializable private string
mName private int mAge private
Person(SerializationInfo aInfo,
StreamingContext aContext) mName
(string)?Info.GetString("Person's name")
mAge aInfo.GetInt32("Person's age")
void ISerializable.GetObjectData(SerializationInfo
aInfo, StreamingContext aContext)
aInfo.AddValue("Person's name", mName)
aInfo.AddValue("Person's age", mAge)
38???????????? ?? ??????????????
using System.Runtime.Serialization Serializable
class Person ISerializable private string
mName private int mAge private
Person(SerializationInfo aInfo,
StreamingContext aContext) mName
(string)?Info.GetString("Person's name")
mAge aInfo.GetInt32("Person's age")
void ISerializable.GetObjectData(SerializationInfo
aInfo, StreamingContext aContext)
aInfo.AddValue("Person's name", mName)
aInfo.AddValue("Person's age", mAge)
39???????????? ?? ??????????????
using System.Runtime.Serialization Serializable
class Person ISerializable private string
mName private int mAge private
Person(SerializationInfo aInfo,
StreamingContext aContext) mName
(string)?Info.GetString("Person's name")
mAge aInfo.GetInt32("Person's age")
void ISerializable.GetObjectData(SerializationInfo
aInfo, StreamingContext aContext)
aInfo.AddValue("Person's name", mName)
aInfo.AddValue("Person's age", mAge)
40???????????? 5
- ????? ???????????? ? ISerializable
41Streaming Contexts
- ??????? ???? ?? ??????????? ???????
- CrossProcess(0x0001), CrossMachines(0x0002),
File(0x0004), Persistence(0x0008),
Remoting(0x0010), Other(0x0020), Clone(0x0040),
CrossAppDomain(0x0080), All(0x00FF) - ?????????? ??????? ?? ????? ???? ?? ???????????,
??????? ?????? ??? ?? ??????? - ?? ??????????? ???????? ???? ?????????
- ?? ?????? ??????????,
42XML ???????????? (??????????)
- ????????????
- XML ????????????
- ????? ? XML ?????????????
- ???????????? ?? ???????? XML
- ??????? ?? ??????????????
43????? ? XML ?????????????
- ??????????? ?? ?????????? ?????? ? XML ?????? ?
??? ?????????? ??? ????????? - XML ?????????????? ????????? ??????
- XML ?????????????? ? ???? ?? ??????????? ?? XML ?
.NET Framework - ????????? ???????????? ?? ???? ???????? ???????!
- XML ?????????????? ?? ???? ?? ?? ?????? ?
???????? ???????? ????? ?? ?????? - XML ?????????????? ???? ?? ??????????? ????????
?????? (???? ??????????) - ??????? ?? ??????????? ??? ?????????
44XML ???????????? ??????
public class Student public string Name
public int Age public void SerializeToXml(Strea
m aStream) XmlSerializer xmlSerializer
new XmlSerializer(typeof(Student))
xmlSerializer.Serialize(aStream, this)
public static Student DeserializeFromXml(
Stream aStream) XmlSerializer
xmlSerializer new XmlSerializer(typeof(St
udent)) return (Student)
xmlSerializer.Deserialize(aStream)
45???????????? 6
46???????????? ?? ???????? XML
- ??????????? ?? ? ????????
using System.Xml.Serialization public class
OptionalOrder XmlElement(ElementName
"Tax_Rate") public decimal TaxRate
XmlAttribute public string FirstOrder
XmlIgnoreAttribute public bool
FirstOrderSpecified XmlArrayAttribute("Items
") XmlArrayItem("MemberName") public
OrderedItem OrderedItems XmlElement
public Employee Employees
47???????????? ?? ???????? XML
- ??????????? ?? ? ????????
using System.Xml.Serialization public class
OptionalOrder XmlElement(ElementName
"Tax_Rate") public decimal TaxRate
XmlAttribute public string FirstOrder
XmlIgnoreAttribute public bool
FirstOrderSpecified XmlArrayAttribute("Items
") XmlArrayItem("MemberName") public
OrderedItem OrderedItems XmlElement
public Employee Employees
48???????????? ?? ???????? XML
- ??????????? ?? ? ????????
using System.Xml.Serialization public class
OptionalOrder XmlElement(ElementName
"Tax_Rate") public decimal TaxRate
XmlAttribute public string FirstOrder
XmlIgnoreAttribute public bool
FirstOrderSpecified XmlArrayAttribute("Items
") XmlArrayItem("MemberName") public
OrderedItem OrderedItems XmlElement
public Employee Employees
49???????????? ?? ???????? XML
- ??????????? ?? ? ????????
using System.Xml.Serialization public class
OptionalOrder XmlElement(ElementName
"Tax_Rate") public decimal TaxRate
XmlAttribute public string FirstOrder
XmlIgnoreAttribute public bool
FirstOrderSpecified XmlArrayAttribute("Items
") XmlArrayItem("MemberName") public
OrderedItem OrderedItems XmlElement
public Employee Employees
50???????????? ?? ???????? XML
- ??????????? ?? ? ????????
using System.Xml.Serialization public class
OptionalOrder XmlElement(ElementName
"Tax_Rate") public decimal TaxRate
XmlAttribute public string FirstOrder
XmlIgnoreAttribute public bool
FirstOrderSpecified XmlArrayAttribute("Items
") XmlArrayItem("MemberName") public
OrderedItem OrderedItems XmlElement
public Employee Employees
51???????????? ?? ???????? XML
- ??????????? ?? ? ????????
using System.Xml.Serialization public class
OptionalOrder XmlElement(ElementName
"Tax_Rate") public decimal TaxRate
XmlAttribute public string FirstOrder
XmlIgnoreAttribute public bool
FirstOrderSpecified XmlArrayAttribute("Items
") XmlArrayItem("MemberName") public
OrderedItem OrderedItems XmlElement
public Employee Employees
52???????????? 7
- ??????? ?? XML ??????????????
53?????? ??????? ?? ??????????????
- ?????????, ??????????? XML ?????????????? ?????
?????? - ??????? ?? ???????? ? ????????
- ???????? ??, ??? ?????? ?????? ?? ???????? ??? ??
????? - ???????????????? ? ?????? ???? ??? ??????????????
?? ?????????????? ? ???????? - ???? ????????? ?? ??????, ???? ?????????? ??
???????? ? ????????
54?????? ??????? ?? ??????????????
- ????????? ?? ???? ????????? XmlAttributeOverrides
? XmlAttributes - ?? ????? ???? ?? ????? ???? ?? ?????? ????????
XmlAttributes, ???????? ??????? ?? ???????? XML - ?????? ??????? ?? ??? ?? ???????
XmlAttributeOverrides overrides new
XmlAttributeOverrides() XmlAttributes attribs
new XmlAttributes() attribs.XmlElements.Add(
new XmlElementAttribute("PersonName")) overrides.
Add(typeof(Person), "Name", attribs) XmlSerialize
r xmlSerializer new XmlSerializer(typeof(Per
son), overrides) ...
55???????????? 8
- ?????? ??????? ?? ??????????????
56???????????? ?? ?????
????????
57?????????? FormatterServices
- ????????? ???? System.Runtime. Serialization.Forma
tterServices - ?????? ?? ????????????
- public static MemberInfo GetSerializableMembers(
Type type) - public static Object GetObjectData(Object obj,
MemberInfo members) - ?????? ?? ??????????????
- public static Type GetTypeFromAssembly( Assembly
assem, String name) - public static Object GetUninitializedObject(Type
type) - public static Object PopulateObjectMembers(Object
obj, MemberInfo members, Object data)
58??????
- ?? ?? ???????? ???? Graph, ????? ?????? ???????
???? (?????????? ???? ????? ?? ???????). ?? ??
???????? ???? Node, ????? ?????? ???? ???? ??
?????. ?????? Node ?????? ?? ???????
????????????? ???? (???????? ????) ? ????? ??
?????????? (????????? ?? ????? ???? Node).
???????????? ??????????????, ????? ??????????? ?
????????????? ????????? ?? ????? Graph. - ???????? ?? ?? ????????????? ??????? ????????? ??
????? System.Collections.Hashtable. ???????? ????
???? ?? ????????????? ???-??????? ? XML
????????????. ????? ???????? ?????????? ?????? ??
?? ???????? ???? XML ?????????????? ?? ???????
?????????? ???????????? ???????.
59??????
- ??????????? ??????? Country ? Town, ?????
???????? ?????????? ?? ??????? ? ???????. ???? ??
???????, ?? ? ???? ??????? ??? ????? ???????.
???????????? ??????? ? XML ???????????? ?
?????????????? ?? ???? ???????. ???????????? TCP
??????, ????? ?? ??? ?? ??????? ????? ??????????
?? ????????? ?????? ? ?????? ??????? ? ??? (???
??? ?? ??????? ???????????? Country ?????).
???????????? Windows Forms ????????? ??????????
?? TCP ???????, ????? ????????? ?? ?? ??????? ?
??????????? ?????????? ?? ?????????. ???????? ?
???????? ?????? ?? ????????? ??? ?????? ?? ??????
? ??????? ???????????? ? ? XML ????????????.
60??????
- ???????? ???? SoapFormatter ???? ?? ???????????
???????? ???? ?? ??????, ? XML ?????????????? ??
????. ???????? ???????? ???????? ???? ?? ??????,
???????????? ?? ?? ????? ?????? ? ????????
????????? XML-?. - ???????? ???? Student ??????? ??????? (????,
?????, ?????, ???????????, ?????????? ?????,
e-mail, ????????? ???????? ? ?????? ?? ?????
???????? ???????). XML ?????????????? ???? ????,
???? ????????? ??????????? ????? ?? ?? ???????
???? XML ???????, ? ?? ???? XML ???????.
???????????? ???????? ??? ??????? ?? ???????? XML
???????? ?? ??? ?????? ? ???????? ? ????? ?
???? XmlAttributeOverrides ????? ?????.
61?????????? ??????????
- MSDN Library
- Object Serialization in the .NET Framework
- System.Runtime.Serialization Namespace
- System.Runtime.Serialization.Formatters Namespace
- System.Runtime.Serialization.Formatters.Binary
Namespace - System.Runtime.Serialization.Formatters.Soap
Namespace - System.Xml.Serialization Namespace
- XmlSerializer Class
- Controlling XML Serialization Using Attributes
- Overriding XML Serialization
62?????????? ??????????
- MSDN Library
- Attributes That Control Encoded SOAP
Serialization - ?ttributes That Control XML Serialization
- The XML Schema Definition Tool and XML
Serialization - Generating SOAP Messages With XML Serialization
- Vyacheslav Biktagirov, .NET Serialization
http//www.csharphelp.com/archives/archive38.html - Mickey Williams, CodeGuru .NET Serialization
http//www.codeguru.com/columns/DotNet/article.php
/c6595/
63?????????? ??????????
- Ali Sufyan, CodeGuru Object Serialization Using
.NET http//www.codeguru.com/Csharp/Csharp/
cs_syntax/serialization/article.php/c7201/ - Jeffrey Richter, .NET Run-time Serialization
MSDN Magazine, April 2002 http//msdn.microsoft.
com/ msdnmag/issues/02/04/net/ - Jeffrey Richter, .NET Run-time Serialization,
Part 2 MSDN Magazine, September 2002
http//msdn. microsoft.com/msdnmag/issues/02/07/ne
t/ - Jeffrey Richter, .NET Run-time Serialization,
Part 3 MSDN Magazine, September 2002
http//msdn. microsoft.com/msdnmag/issues/02/09/ne
t/