I'm connected to a websocket and subscribed to the MessageRecieved event. In that event I will get a string (json) that could be deserialized to any of about 5 different DataContracts. Right now I'm just using a switch statement with a unique bit of the txt and then calling var item = message.FromJson<MyType1>();
Is there a better way to do that dynamically?
If you want the JSON deserialized into a Type then you would need to tell the ServiceStack serializer what type it should deserialize into, if you prefer you can deserialize into a runtime type with:
Type intoType = GetTypeToDeserializeInto(msg);
var dto = JsonSerializer.DeserializeFromString(msg, intoType);
Alternatively if you just need to generically access the JSON data without needing to deserialize into a Type you can use JSON Utils, eg:
var obj = (Dictionary<string, object>) JSON.parse(msg);
var prop1 = obj["prop1"];
If the JSON is a flat structure you can later use ServiceStack's Reflection Utils to convert the object dictionary into a Type, e.g:
var dto = obj.FromObjectDictionary<MyType1>();
var dto = map.FromObjectDictionary(intoType); // runtime Type
If all message Types are derived from the same subclass it's also possible to embed the Type the JSON should deserialize into if it embeds the Full Type Name in the first "__type" property, e.g:
// json starts with: {"__type": "Namespace.MyType1", ...}
var dto = msg.FromJson<Message>(json);
But you would need to take into consideration of restrictions imposed on auto deserialization of Late-bound Object Types.