Why should I explicitly use JValue for the target type before using it?

I have a code like this:

var jsonSerializer = new JsonSerializer(); var json = @"{ ""LastModifiedTime"": ""2013-04-19T18:18:09+03:00"" }"; var result = jsonSerializer.Deserialize<dynamic>(new JsonTextReader(new StringReader(json))); File.SetLastWriteTime("c:/temp/somefile.txt", result.LastModifiedTime); 

However, this gives me (at runtime, since we are talking about dynamics here):

 RuntimeBinderException The best overloaded method match for 'System.IO.File.SetLastWriteTime(string, System.DateTime)' has some invalid arguments 

This is a little stupid. For me, a bit of a dynamic point, there is no need to explicitly specify types, etc .; this should (ideally) be handled by runtime. Of course, for JSON.NET it can be a little difficult to find out what it should mask, as in this situation ...

James (or someone else familiar with internal JSON.NET), is this by design? If I add a manual DateTime statement as follows:

 File.SetLastWriteTime("c:/temp/somefile.txt", (DateTime) result.LastModifiedTime); 

... everything is working fine.

+6
source share
1 answer

Found the answer here right before posting ...

Turns out I need to either quit or use the Value JValue property. However, some details about this would be good. Of course, just being able to magically use my JValue as a parameter to a third-party method, without worrying about casting, would be optimal, providing the most “dynamic language” that is similar to the experience (otherwise, really great!) JSON functionality. NET

+3
source

All Articles