I am (very) new to C#, and I am trying to implement an existing application metamodel in this language. In this metamodel, a “menus” table holds a combination of forms and actions, indicating, for each form, which actions should be available.
I am then able to use this “menus” datasource to build and display a form’s specific MenuStrip at runtime. Each item in the menu (ToolStripMenuItem) holds the corresponding ‘action’ string. Clicking on this item should launch the corresponding command.
So now is the question: how do I link the action string of the ToolStripmenuItem to the corresponding command?
My first idea was to maintain a HashTable containing the action string as a key, and the method to be executed, like this:
a generic CommandHandler method:
delegate void CommandHandler(string[] args);
and, in my main module, a HashTable looking like this
myHashCommandTable.Add
( "issueThePurchaseOrder",
new CommandHandler(PurchaseOrder.IssueThePurchaseOrder))
I could then run the corresponding command at runtime:
if myHashCommandTable.ContainsKey(myActionString)
{
((CommandHandler)myHashCommandTable[myActionString])
}
But this would force me to maintain this HashTable “by hand”, and it could quickly become a source of trouble for the developpers, and instability for the application! Any other idea?
I guess you could just store the name of target method in your data source and then use reflection to look it up at run-time. Passing data to the target method would be tricky, but that’s just one more exciting challenge for you to overcome!