Bonjour,
Je souhaite sérialiser des classes dans le but de les enregistrer dans un fichier XML.
Ca marche tres bien ... jusqu'au moment ou je crée un constructeur.....
En effet, des que j'ai crée un constructeur pour une des classes, et bien le processus de serialisaton me crée une exception :
Alors voila, je vois pas trop quel est le prob..;
Code : Sélectionner tout - Visualiser dans une fenêtre à part Une erreur s'est produite lors de la réflexion du type 'essaiXML.Slideshows
Je pourrai faire sans constructeur, mais c pas très propre....
Voici pour les declarations de classes :
Code VB.NET : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 Public Class Slideshows Public slideshow As slideshow() Public Function getNBSlideshows() As Integer ' retourne le nb de diaporama Return Me.slideshow.Length End Function End Class Public Class slideshow <XmlAttribute()> _ Public titre As String = "Small bowel resection & anastomosis in the pig" Public id As Integer = 0 Public MD5 As String = "5A4E21BAC397E555F5" Public type As String = "Powerpoint" Public version As String = "11.0" Public date_de_modification As Date Public date_de_creation As Date Public derniere_diffusion As Date Public NBDiffusions As Integer = 0 Public chapitres As chapitre() Public fichiers As fichier() Public Function donneNBFichiers() As Integer Return Me.fichiers.Length End Function Public Function donneNBChapitres() As Integer Return Me.chapitres.Length End Function Public Function AjouteChapitre(ByVal intitule As String, ByVal page As Integer, ByVal active As Boolean) As Boolean Dim nbChapitres As Integer If Not Me.chapitres Is Nothing Then nbChapitres = Me.chapitres.Length Else nbChapitres = 0 End If ReDim Preserve chapitres(nbChapitres) chapitres(nbChapitres) = New chapitre(intitule, page, active) End Function End Class Public Class chapitre <XmlAttribute()> _ Public intitule As String Public page As Integer Public active As Boolean Sub New(ByVal intitule As String, ByVal page As Integer, ByVal active As Boolean) Me.intitule = intitule Me.page = page Me.active = active End Sub End Class Public Class fichier Public nom As String = "video.mpg" Public MD5 As String = "5A4E21BAC397E555F5" Public taille As Double = 0 Public date_de_modification As Date Public date_de_creation As Date End Class
et voila pour le code :
Code VB.NET : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 Dim obj As New Slideshows Dim ppt As New slideshow 'ppt.titre = "Texte" ReDim obj.slideshow(2) obj.slideshow(0) = ppt obj.slideshow(1) = ppt obj.slideshow(2) = ppt ReDim obj.slideshow(0).fichiers(1) obj.slideshow(0).fichiers(0) = New fichier obj.slideshow(0).fichiers(1) = New fichier obj.slideshow(0).AjouteChapitre("Chapitre1", 22, True) Dim monSerializer As XmlSerializer = New XmlSerializer(GetType(Slideshows)) Dim mywriter As StreamWriter = New StreamWriter("E:\Documents and Settings\Matt\Bureau\FichierXLM.xml") monSerializer.Serialize(myWriter, obj) myWriter.Close()
C'est le constructeur "Sub New " qui pose probleme, et qui souleve l'exception lors du Dim monSerializer As XmlSerializer = New XmlSerializer(GetType(Slideshows))







Répondre avec citation
Partager