Avoir un objet grace à un string en c#
glob04d
-
Krysstof Messages postés 1659 Statut Membre -
Krysstof Messages postés 1659 Statut Membre -
Bonjour à tous,
j'ai une question bete je crois, j'aimerais créer une sorte de liste ( ou de tableau), qui me lit un objet grace a une clé (un string)
Par exemple;
sortelist ( string ma cle, objet monobjet).
initialisation :
sortelist ("jpg", System.Drawing.Imaging.ImageFormat.jpeg)
sortelist ("gif", System.Drawing.Imaging.ImageFormat.gif)
sortelist ("png", System.Drawing.Imaging.ImageFormat.png)
et comme résultat, j'aimerais avoir :
sortelist("jpg") me retourne l'objet System.Drawing.Imaging.ImageFormat.jpeg
Je vous remercie
j'ai une question bete je crois, j'aimerais créer une sorte de liste ( ou de tableau), qui me lit un objet grace a une clé (un string)
Par exemple;
sortelist ( string ma cle, objet monobjet).
initialisation :
sortelist ("jpg", System.Drawing.Imaging.ImageFormat.jpeg)
sortelist ("gif", System.Drawing.Imaging.ImageFormat.gif)
sortelist ("png", System.Drawing.Imaging.ImageFormat.png)
et comme résultat, j'aimerais avoir :
sortelist("jpg") me retourne l'objet System.Drawing.Imaging.ImageFormat.jpeg
Je vous remercie
5 réponses
-
juste pour confirmation,
System.Drawing.Imaging.ImageFormat.png est un type
"System.Drawing.Imaging.ImageFormat.png" est une chaine de caractère.
quand tu dit "sortelist("jpg") me retourne l'objet System.Drawing.Imaging.ImageFormat.jpeg
"
tu veux quoi exactement en retour, le nom du type, une chaine de caractère représentant le nom du type, ou un objet vide de ce type? -
-
en fait c'est pour mettre dans cette fonction la :
string cheminphoto = "C:\\...."
myBitmapResize.Save(cheminphoto, sortelist("jpg")).
Vu que save me demande:
(string string, System.Drawing.Imaging.ImageFormat format) -
Salut,
utilise un hashtable...
ashtable ht = new Hashtable();
ht.Add("jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
ht.Add("gif", System.Drawing.Imaging.ImageFormat.Gif);
ht.Add("png", System.Drawing.Imaging.ImageFormat.Png);
ImageFormat t=(ImageFormat)ht["jpg"];
@+ -
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question -
Si tu utilse un dictionnaire, tu n'a pas besoin de "typer" ton résultat contrairement a une hashtable
System.Collections.Generic.Dictionary<string, System.Drawing.Imaging.ImageFormat> listetype =
new System.Collections.Generic.Dictionary<string, System.Drawing.Imaging.ImageFormat>();
listetype.Add("jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
listetype.Add("gif", System.Drawing.Imaging.ImageFormat.Gif);
listetype.Add("png", System.Drawing.Imaging.ImageFormat.Png);
myBitmapResize.Save(cheminphoto, listetype["jpg"]);