此代码:无法投射型 'Newtonsoft.Json.Linq.JObject' 的目的是键入<MyClass>
var commandMessage = new CommandMessage { CorrelationId = Guid.NewGuid() };
var json = JsonConvert.SerializeObject(commandMessage);
var myCommandMessage = (CommandMessage)JsonConvert.DeserializeObject(json);
给出了这样的错误消息:
Additional information: Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'QueueConnectionStringTester.CommandMessage'
这是对CommandMessage类:
public class CommandMessage
{
public Guid CorrelationId { get; set; }
}
我在这里错过了什么?
你可以显示你的'CommandMessage'类吗? –
是的,我发布了CommandMessage类的代码。它有:-) –
为什么不只是使用'JsonConvert.DeserializeObject(json,commandMessage.GetType());'?看起来像那是什么文件要你做:http://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonConvert_DeserializeObject.htm – KreepN