JToken jToken = JToken.Parse("{ \"meta\": { \"status\": 200 } }");
if (jToken.SelectToken("meta.status") is long status)
Console.WriteLine("It's a boy!");
gives compile error:
CS8121 An expression of type 'JToken' cannot be handled by a pattern of type 'long'.
but this compiles without error:
JToken jToken = JToken.Parse("{ \"meta\": { \"status\": 200 } }");
if ((long)jToken.SelectToken("meta.status") is long status)
Console.WriteLine("It's a boy!");
dotnet issue 16195 indicates that pattern matching should work if an explicit conversion exists, so what am I missing?
I'm testing with Newtonsoft.Json 12.0.3 and in:
Visual Studio 16.7.7 (C# 7.3 as per #error version)
Visual Studio 15.9.28 (C# 7.1 set in project properties)
Edit: This appears to be a more robust way of doing the test, but I still don't understand why I have to tell C# the JToken is a long before it will test if it's a long.
JToken jToken = JToken.Parse("{ \"meta\": { \"status\": 200 } }");
if (jToken.SelectToken("meta.status")?.Value<long>() is long status)
Console.WriteLine("It's a boy!");