I have a message coming into my C# app which is an object serialized as JSON, when i de-serialize it I have a \"Name\" string
and a \"Payload\" string[]
Something similar to this:
public class Methods
{
public readonly Dictionary> MethodsDict = new Dictionary>();
public Methods()
{
MethodsDict.Add("Method1", Method1);
MethodsDict.Add("Method2", Method2);
}
public string Execute(string methodName, string[] strs)
{
Func method;
if (!MethodsDict.TryGetValue(methodName, out method))
{
// Not found;
throw new Exception();
}
object result = method(strs);
// Here you should serialize result with your JSON serializer
string json = result.ToString();
return json;
}
public object Method1(string[] strs)
{
return strs.Length;
}
public object Method2(string[] strs)
{
return string.Concat(strs);
}
}
Note that you could make it all static
, if the methods don't need to access data from somewhere else.
The return type I chose for the delegates is object
. In this way the Execute
method can serialize it to Json freely.