jeudi 3 août 2017

Reduce IF-Statements and make Method-Calls dynamically

I feel like I searched the whole web for an Answer.

I have a Problem (first of all maybe with my english so please excuse me. You can keep all mistakes you'll find :D).

It's about These Code-Snippet :

public delegate string[] Del();

    private string[] getMenuItems(string name)
    {
        //Del returnMethod = typeof(Menü).GetMethod(name+"MenuItem") ;

        //DynamicMethod dm = new DynamicMethod(name + "MenuItem", typeof(string[]), null, typeof(Menu));
        //Del returnMethod = (Del)dm.CreateDelegate(typeof(Del));

        //return returnMethod();
        //if (name == menus.Tipp.ToString())
        //    return TippMenuItems();

        //else if (name == menus.Administration.ToString())
        //    return AdministrationMenuItems();

        //else if (name == menus.Help.ToString())
        //    return HelpMenuItems();

        //else if (name == menüs.Usermanagement.ToString())
        //{
        //    return UsermanagementMenuItems();
        //}

        //else return null;

    }

    private string[] TippMenuItems()
    {
        return new string[] { "Spieltag Ansehen", "Platzierung Bei Tipp", "Tipp Abgeben" };
    }

    private string[] AdministrationMenuItems()
    {
        return new string[] { "Ergebnis Anlegen", "Mannschaft Anlegen", "Paarung Anlegen" };
    }

    private string[] UsermanagementMenuItems()
    {
        if (Login.benutzer.Rollen.Find(rollen => rollen.Name == "Full Admin") != null)
            return FullAdminMenuItems();
        else
            return SmallAdminMenuItems();
    }

    private string[] FullAdminMenuItems()
    {
        return new string[] { "Benutzer Anzeigen", "Als Benutzer Handeln", "Benutzer Anlegen", "Passwort Zurücksetzen" };
    }

    private string[] HelpMenuItems()
    {
        return new string[] { "Kontakt", "FAQ", "About" };
    }

    private string[] SmallAdminMenuItems()
    {
        return new string[] { "Benutzer Anzeigen", "Passwort Zurücksetzen" };
    }

I want to make it small and sexier than this. Just one call in the getMenuItems-Method but every of the *MenuItems should be called.

I think I'm bad in Explanation so I tried something with Delegates and Dynamic Methods to Show, what I thought I could do.

Do you have any Ideas why this doesn't work or where I have to look?

Thank you!

Christoph


EDIT Maybe this: I want to reduce the IF-Structure to a "simple" dynamic Method-Call. "Name" is "Tipp", "Administration", "Help" or "Usermanagement".

I got the same Problem with other method-constructions so a "rule"/"template" would be very helpfull...

Aucun commentaire:

Enregistrer un commentaire