81
Chương 2: Thao tác dữ liệu
this.List.Remove(value);
}
public AssemblyName this[int index] {
get {
return (AssemblyName)this.List[index];
}
set {
this.List[index] = value;
}
}
public bool Contains(AssemblyName value) {
return this.List.Contains(value);
}
public void Insert(int index, AssemblyName value) {
this.List.Insert(index, value);
}
}
P
CollectionBase
DictionaryBase
=.6M:$z
&zFINK
On*
PM:'g~*
OnClear
8
OnClearComplete
8
OnGet
8
OnGetComplete
8•h $X1C)3;5.X&*
=:ve'&INI9B6$.*P
CollectionBase
DictionaryBase
@0M:e$JO$=
>6$5.)492
List
'
Dictionary
12.
12.
L u m t đ i t ng kh -tu n-t -hóa vào fileư ộ ố ượ ả ầ ự
L u m t đ i t ng kh -tu n-t -hóa vào fileư ộ ố ượ ả ầ ự
Bạn cần lưu một đối tượng khả-tuần-tự-hóa và các trạng thái của nó vào file, sau
đó giải tuần tự hóa khi cần.
Sử dụng một
formatter
để tuần tự hóa đối tượng và ghi nó vào một
System.IO.FileStream
. Khi cần truy xuất đối tượng, sử dụng
formatter
cùng kiểu
để đọc dữ liệu được-tuần-tự-hóa từ file và giải tuần tự hóa đối tượng. Thư viện
82
Chương 2: Thao tác dữ liệu
lớp
.NET Framework
cung cấp các hiện thực
formatter
sau đây để tuần tự hóa
đối tượng theo dạng nhị phân hay
SOAP
:
•
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
•
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
y
BinaryFormatter
SoapFormatter
F$O)N=F.D$
&…$/!2
System.SerializableAttribute
BinaryFormatter
.(. )K i<.4 D$ * F8
SoapFormatter
.SOAP
P
BinaryFormatter
SoapFormatter
=)
System.Runtime.
Serialization.IFormatter
8 ) ' i q M :\
Serialize
Deserialize
•
Serialize
6..I
System.Object
..I
System.IO.Stream
.DD8N=F
Object
F
Stream
•
Deserialize
6..I
Stream
.DD80)KD$$z
Nz=zFQ
Stream
8..I
Object
ID$$zzNz
=zFL*X.I
Object
'2$
l0M:
Serialize
Deserialize
BinaryFormatter
8.S&*
$ NO
SerializationFormatter
System.Security.Permissions.
SecurityPermission
l0M:
Serialize
Deserialize
SoapFormatter
8.S&*
$ 9' + C ' D- gfull trusth % (.&' System.Runtime.
Serialization.Formatters.Soap.dllg
SoapFormatter
$&&>(.&''h
4X.SJ$zz6'z.zNgpartially trusted callerhO)F
7..Z^ZF>.4.S$zz6'z.zN
y
BinarySerializationExample
)<'.0O)
BinaryFormatter
N=F.
System.Collections.ArrayList
:)> .f(|
F8
ArrayList
$N=FQf()F@$iOs
Console
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
public class BinarySerializationExample {
public static void Main() {
// Tạo và cấu hình ArrayList để tuần tự hóa.
ArrayList people = new ArrayList();
people.Add("Phuong");
83
Chương 2: Thao tác dữ liệu
people.Add("Phong");
people.Add("Nam");
// Tuần tự hóa đối tượng ArrayList.
FileStream str = File.Create("people.bin");
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(str, people);
str.Close();
// Giải tuần tự hóa đối tượng ArrayList.
str = File.OpenRead("people.bin");
bf = new BinaryFormatter();
people = (ArrayList)bf.Deserialize(str);
str.Close();
// Hiển thị nội dung của đối tượng ArrayList
// đã-được-giải-tuần-tự-hóa.
foreach (string s in people) {
System.Console.WriteLine(s);
}
}
}
L* F O )
SoapFormatter
( $ % &'
BinarySerializationExample
C>8JN'.#
BinaryFormatter
&5
SoapFormatter
' s J i
using
6 4 >
System.Runtime.Serialization.Formatters.Soap
H8&*N>...I
I System.Runtime.Serialization.Formatters.Soap.dll &> )i .S „(
SoapSerializationExample.cs q PU 2 1. @ % &' O )
SoapFormatter
n%]Z]])<'.0I9O)
BinaryFormatter
SoapFormatter
n%]Z%&')f( people.bin $*O)
BinaryFormatter
8%]]%&')f(people.xml$*O)
SoapFormatter
84
Chương 2: Thao tác dữ liệu
Hình 2.1
Nội dung file people.bin
Hình 2.2
Nội dung file people.xml
85
Chương 2: Thao tác dữ liệu
3
86
Chương 3:MIỀN ỨNG DỤNG, CƠ CHẾ PHẢN CHIẾU, VÀ
SIÊU DỮ LIỆU
87
88
Chương 3: Miền ứng dụng, cơ chế phản chiếu, và siêu dữ liệu
:.*2*Microsoft .NET Framework$<&Cv
.>)KG=P.M'@
%&'2*4).:)gapplication domainh8MI
Igreflectionh8>)Kgmetadatah8&t.\
|
7*'.:)g.^Z^ch
y.D$O).:)g.^]8
^^8^_8^bh
y.4
Type
g.^Zd^ZZh
7*D$*(.&'G=g.^`8^a8^[8
^Z]h
7*.!2e'&Ig.^Z^^Z_h
1.
1.
T o mi n ng d ngạ ề ứ ụ
T o mi n ng d ngạ ề ứ ụ
Bạn cần tạo một miền ứng dụng mới.
Sử dụng phương thức tĩnh
CreateDomain
của lớp
System.AppDomain
.
U*:MM:
CreateDomain
6.DD
string
Ji
><.:).P)*:X&*Ji::
gevidencehI6%.:).P::$Ji&5D
$
System.Security.Policy.Evidence
T.Z^ZZ%&'::
&* * . . : ) P I 6 % $ J i &5 D $
System.AppDomainSetup
y
AppDomainSetup
:4%..:)L^Z>
2 $O)
AppDomainSetup
*.:)
P2'F$';*49>D$
AppDomain
8 . D F ' s G =T &* S' . NET
Framework SDK
AppDomain
IM
Bảng 3.1
Các thuộc tính thông dụng của lớp AppDomainSetup
Thuộc tính Mô tả
ApplicationBase
7..CRL@;X9%)k%.(.&'
>w€6)k%.gprobingh@$6.
^`7=I8
ApplicationBase
.D:)
=7(.!i8<'.:(.&'
PF0$2'*.:)
&52
AppDomain.BaseDirectory
89
Chương 3: Miền ứng dụng, cơ chế phản chiếu, và siêu dữ liệu
ConfigurationFile
7>f(%8$O)&C.SS$*
.:)PF0$2'*
.:)&5M:
AppDomain.GetData
F
APP_CONFIG_FILE
DisallowPublisherPolicy
Y'iNpublisher policyf(%:)
F$;XI'4;i>&.
(.&'>.*DIPublisherpolicy@$
6.^`
PrivateBinPath
U.&C).x'.&
=@O))k%.(.&'>P
.'Fi2MD.$Ji
ApplicationBase
PF0$2'
* . : ) &5 2
AppDomain.RelativeSearchPath
PF's2
'G=&5M:
AppendPrivatePath
ClearPrivatePath
u2))<'%&'*%..:)\
// Khởi tạo một đối tượng của lớp AppDomainSetup.
AppDomainSetup setupInfo = new AppDomainSetup();
// Cấu hình các thông tin cài đặt cho miền ứng dụng.
setupInfo.ApplicationBase = @"C:\MyRootDirectory";
setupInfo.ConfigurationFile = "MyApp.config";
setupInfo.PrivateBinPath = "bin;plugins;external";
// Tạo một miền ứng dụng mới (truyền null làm đối số chứng cứ).
// Nhớ lưu một tham chiếu đến AppDomain mới vì nó
// không thể được thu lấy theo bất kỳ cách nào khác.
AppDomain newDomain = AppDomain.CreateDomain(
"My New AppDomain",
new System.Security.Policy.Evidence(),
setupInfo);
Bạn phải duy trì một tham chiếu đến đối tượng
AppDomain
vừa tạo bởi vì không
có cơ chế nào để liệt kê các miền ứng dụng hiện có từ bên trong mã được-quản-
lý.
90
Chng 3: Min ng dng, c ch phn chiu, v siờu d liu
2.
2.
Chuy n cỏc i t ng qua l i cỏc mi n ng d ng
Chuy n cỏc i t ng qua l i cỏc mi n ng d ng
Bn cn chuyn cỏc i tng qua li gia cỏc min ng dng nh cỏc i s hay
cỏc giỏ tr tr v.
S dng cỏc i tng
marshal-by-value
hay
marshal-by-reference
.
nDNET Remotingg@$6MZ]hGCD$9
ĂÂÊÔƠƯĐÊăƠâêôÊơ-đĂNETRemoting8I
9F.$7=8<'Fv)e.:
)=MNET Remoting:<'D$9.
:)
7)KF&*\nonremotable8marshal-by-valuegMBVh8
marshal-by-referencegMBRhwnonremotable4$9&>.:)
4)e.DD'i 0.4 >.
:)wnonremotable@$6.^_
wMBVzNz=zFw.D$MBV$'9..
:)DD'i8D.NET Remoting@N=F*
*D$8')KF.:)28*.&
D$e*D$DwI9t*&D$
C.:)nD$'&ND86
8>'sD$'4CID$U<'2)
.zNz=zFF>
Employee
8$'9..:)&5
ig;(..ZaZ&I*zNz=zFh
[System.Serializable]
public class Employee {
// Hin thc cỏc thnh viờn õy.
Đ
}
wMBR)3;Q
System.MarshalByRefObject
w.D$MBR
$'9..:)DD'i8D .NET
Remoting@*.D$;'D$MBVN'.:)
2lD$*)=DD$MBR.F*
)7=8=.>D$*)8DNET Remoting
=N.' 0DDNII.:)t8*
F= 0.>D$MBRDwI9$49D$
*)U<'.>&
Employee
8$'9..:
)&5.I'%&5ig;(..Z][&II*
MBRh
public class Employee : System.MarshalByRefObject {