3

I'd like to be able to store a dictionary of Actions and being able to invoke those based on key for the dictionary. If key is not found, then no action is getting invoked. I had an idea of performing invocation using an object that extends DynamicObject and get dictionary of actions at construction time, but then all examples of how to use DynamicObject are based on properties, not methods. I'd like to be able to do something like this:

`dynamicDictionary.InvokeMethod(commandName);`

1 Answer 1

4

The opensource framework Dynamitey will do this. For a Dictionary of actions you can either construct using an MS built-in ExpandoObject or Dynamitey has an DynamicObjects.BaseDictionary that will work like a proxy to a dictionary.

If you fill either with actions as properties they can be invoked as methods. If you need to invoke them by a string name, you can use Dynamic.InvokeAction(dynamicDictionary, commandName). This uses the DLR to invoke a method it's faster than reflection on Static objects and make it's possible on Dynamic Objects.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to setup a default action, so that when a requested actions is not found, default would kick in?
@Sean just subclass ImpromptuDictionary and override TryInvokeMember` then you can call the base implementation and if it returns false do your default action and return true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.