Avoir un objet grace à un string en c#

glob04d -  
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

5 réponses

  1. Krysstof Messages postés 1659 Statut Membre 295
     
    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?
    0
  2. glob04d
     
    je veux qui me retourne le type, et non une chaine de caractere
    0
  3. glob04d
     
    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)
    0
  4. chuka Messages postés 980 Statut Membre 379
     
    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"];
    @+
    0
  5. Vous n’avez pas trouvé la réponse que vous recherchez ?

    Posez votre question
  6. Krysstof Messages postés 1659 Statut Membre 295
     
    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"]);
    0