此代碼:無法投射型 '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