Tải bản đầy đủ (.pdf) (10 trang)

Các giải pháp lập trình CSharp- P9 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (2.67 MB, 10 trang )

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
=.6M:$z
&zFINK 
On*
PM:'g~* 
OnClear
8 
OnClearComplete
8


OnGet
8
OnGetComplete
8•h $X1C)3;5.X&*
=:ve'&INI9B6$.*P
CollectionBase

DictionaryBase
@0M:e$JO$=
>6$5.)492
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
.DD8N=F
Object
F
Stream


Deserialize

6..I
Stream
.DD80)KD$$z

Nz=zFQ
Stream
8..I
Object
ID$$zzNz
=zFL*X.I
Object
'2$
l0M:
Serialize

Deserialize

BinaryFormatter
8.S&*
 $  NO 
SerializationFormatter
  
System.Security.Permissions.
SecurityPermission

l0M:
Serialize

Deserialize

SoapFormatter
8.S&*
  $    9'  +  C  '  D-  gfull trusth  %  (.&' System.Runtime.
Serialization.Formatters.Soap.dllg

SoapFormatter
$&&>(.&''h
4X.SJ$zz6'z.zNgpartially trusted callerhO)F
7..Z^ZF>.4.S$zz6'z.zN
y
BinarySerializationExample
)<'.0O)
BinaryFormatter

N=F.
System.Collections.ArrayList
:)> .f(|
F8
ArrayList
$N=FQf()F@$iOs
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>8JN'.#
BinaryFormatter
&5
       
SoapFormatter
   '  s  J  i 
using

   6  4    >
System.Runtime.Serialization.Formatters.Soap
H8&*N>...I
I System.Runtime.Serialization.Formatters.Soap.dll   &>  )i  .S  „(
SoapSerializationExample.cs   q  PU  2  1.  @  %  &'    O  )  
SoapFormatter

n%]Z]])<'.0I9O)
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$<&Cv
.>)KG=P.M'@
%&'2*4).:)gapplication domainh8MI
Igreflectionh8>)Kgmetadatah8&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*.!2e'&Ig.^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*:MM:
CreateDomain
6.DD
string
Ji
><.:).P)*:X&*Ji::
gevidencehI6%.:).P::$Ji&5D
$
System.Security.Policy.Evidence
T.Z^ZZ%&'::
&*  *  .  .  :  )  P  I  6   %  $  J  i  &5  D  $
System.AppDomainSetup

y
AppDomainSetup
:4%..:)L^Z>
2 $O)
AppDomainSetup
*.:)
P2'F$';*49>D$
AppDomain
8  . D F  ' s G =T &* S' .   NET
Framework SDK
AppDomain
IM

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@;X9%)k%.(.&'
>w€6)k%.gprobingh@$6.
^`7=I8
ApplicationBase
.D:)
=7(.!i8<'.:(.&'
PF0$2'*.:)
&52
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.SS$*
.:)PF0$2'*
.:)&5M: 
AppDomain.GetData
 
F
APP_CONFIG_FILE

DisallowPublisherPolicy
Y'iNpublisher policyf(%:)
F$;XI'4;i>&.
(.&'>.*DIPublisherpolicy@$
6.^`

PrivateBinPath
U.&C).x'.&
=@O))k%.(.&'>P
.'Fi2MD.$Ji

ApplicationBase
PF0$2'
  *  .  :  )  &5    2
AppDomain.RelativeSearchPath
PF's2
'G=&5M: 
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 {

×