I was wondering if it was possible to call a function on a dynamic c# object via a string. I want to do something like this:
string lMethodName = "MethodName";
dynamic lDynamicObject = GetDynamicObject();
lDynamicObject.Invoke(lMethodName, lParameters);
Any thoughts or solutions would be greatly appreciated.
Maybe like Servy said there's a better way to achieve what we want through a different design. We're using SignalR and have a dashboard web app. We want each widget inside the dashboard to have the ability to have data pushed to it. To achieve this we were going to use razor code to inject the widget id into each individual widgets SignalR clientside proxy. Each widget would have something like the following:
hub.client.updateWidget123456 = aUpdateFunction;
Where 123456 is the id of the widget. On the serverside SignalR hub code we could call back into the clientside javascript function:
int lWidgetId = 123456;
dynamic lDynamic = Clients.All;
lDynamic.Invoke("updateWidget" + lWidgetId, lParameters);
There are other ways I can think of to implement this without creating the javascript proxy methods. I thought this was nice because the server had a direct communication line with each individual widget. Which could be accessed from multiple dashboards/browsers but they would all get updated via the single call above.
We could implement this instead by calling some other object in our javascript clientside code who knew about all the widgets and how to direct information to them. I felt like this went against the hub architecture of SignalR and like we were reinventing the wheel by doing this.
Does anyone have any thoughts on a better design approach? Thanks for the help and thanks for your comment Servy for sparking this discussion.
Also, this question is not a duplicate. The other question is not with regards to dynamic objects like Paul commented on below.