0

I'm trying to create a menu class, where i can add MenuItems within my code and then call the subitem via static Properties.

My idea:

var myMenu = new Menu();
myMenu.Add("Articles");
myMenu.Add("Customers");

// now call the item via...
_navigation.NavigateTo(Menu.Articles);

// or add further subitems
Menu.Customers.Add("International");

Can C# build such properties?

Daniel
  • 511
  • 10
  • 25
  • Can't comment questions yet, but try to look up here http://stackoverflow.com/questions/6196022/adding-properties-dynamically-to-a-class http://stackoverflow.com/questions/947241/how-do-i-create-dynamic-properties-in-c – yolo sora Jan 23 '17 at 10:13

1 Answers1

0

You could create static properties for that. You need to store the instances in the static properties. They can't be dynamically created. If you want to access them dynamically, you should create something like a Dictionary<string, MenuItem> to access instances by name.

public class Program
{
    static void Main()
    {
        MyMenu = new Menu();
        Articles = myMenu.Add("Articles");
        MyMenu.Add("Customers");

    }

    public static Menu MyMenu {get; private set;}
    public static MenuItem Articles { get; private set;}
}

You can choose if you want to access the Menu or the MenuItem. You can access them by using Program.MyMenu or Program.Articles etc.

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • I think I'll go the way with predefined static Properties as you mentioned. The idea with ExpandoObject looked very nice, but when i need to read the Expando Properties i have to cast my menu Object to `dynamic`, so intellisense doesn't like it anymore... – Daniel Jan 26 '17 at 08:23