déclaration d'objet paramétrée
Bonjour à tous !
Je commence à développer en C# et je me confronte déjà à pas mal de problèmes :
J'ai créé une classe sérialisable que voici :
Code:
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
| using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.IO;
namespace mysql
{
public class connexion:ISerializable
{
public string login;
public string mdp;
public string serveur;
public connexion(string sLogin, string sMdp, string sServeur)
{
login = sLogin;
mdp = sMdp;
serveur = sServeur;
}
public connexion() : this("","",""){
}
public connexion(string chemin)
{
mysql.traitements.chargement.chargeXml(chemin, this);
}
//public void chargeXML(string chemin)
//{
// if (File.Exists(chemin))
// {
// XmlSerializer serializer = new XmlSerializer(typeof(connexion));
// TextReader reader = new StreamReader(chemin);
// connexion maConnexion;
// maConnexion = (connexion)serializer.Deserialize(reader);
// login = maConnexion.login;
// reader.Close();
// }
//}
void ISerializable.GetObjectData(SerializationInfo oInfo, StreamingContext oContext)
{
oInfo.AddValue("login", this.login);
oInfo.AddValue("mdp", this.mdp);
oInfo.AddValue("serveur", this.serveur);
}
}
} |
Le fais d'avoir une méthode de chargement propre à chacune de mais classe ne me paraissait pas terrible, j'essaye donc de concevoir une fonction générique permettant de charger un objet à partir d'un fichier XML.La fonction "chargeXML" de ma classe connexion fonctionnait j'essaye de la rendre générique à fin de pouvoir charger n'importe quel type d'objet.
J'ai donc créé une classe chargement que voici :
Code:
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
|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Reflection;
namespace mysql.traitements
{
public class chargement
{
public static void chargeXml(string chemin, object oObject)
{
if (File.Exists(chemin))
{
XmlSerializer serializer = new XmlSerializer(oObject.GetType());
TextReader reader = new StreamReader(chemin);
object monObjet;
monObjet = (object)serializer.Deserialize(reader);
// Boucle permettant de charger les attributs de l'objet à partir de l'XML
PropertyInfo[] myPropertyInfo, myPropertyInfo2;
// Get the properties of 'Type' class object.
myPropertyInfo = Type.GetType(monObjet.GetType().ToString()).GetProperties();
for (int i = 0; i < myPropertyInfo.Length; i++)
{
Console.WriteLine(myPropertyInfo[i].ToString());
//login = maConnexion.login;
//mdp = maConnexion.mdp;
//serveur = maConnexion.serveur;
}
reader.Close();
}
//else
//{
// login = "";
// mdp = "";
// serveur = "";
//}
}
}
} |
Mes problèmes sont les suivants :
1 Je ne parviens pas à lister les propriétés de mon objet :
Code:
myPropertyInfo = Type.GetType(monObjet.GetType().ToString()).GetProperties();
me retourne un tableau vide !
2 Je n'ai aucune idée de comment affecter la valeur charger à partir de mon xml à la propriété de mon objet :
Code:
oObjet.(myPropertyInfo[i].ToString()) = monObjet.(myPropertyInfo[i].ToString())
Si vous pouviez m'éclairer un peu ce serait bien sympathique, n'hésitez pas à me donner des conseils sur l'approche du code, comme je l'expliquais je débute et je me doute bien que la lecture de mon code doit piquer les yeux avertis.
Merci d'avance