Click here to Skip to main content
14,937,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my block of code where i'm getting issue like -
"Execution cannot reach the expression void() inside this statement logContext?.Add(kvp.Key((..."

C#
var dict = JsonConvert.DeserializeObject<dictionary<string, object="">>(jsonString);                 
var logContext = new JObject();                 
foreach (var kvp in dict)                 
{                     
logContext?.Add(kvp.Key, kvp.Value as JToken ?? new JValue(kvp.Value));                 
}


What I have tried:

any idea, what i'm missing and to modify, plz suggest ?
Posted
Updated 11-Jun-21 7:04am
v2

When I try it (in a slightly "cut down" form as I don't have your JSON data) I get no such error:
C#
var dict = new Dictionary < string, object>();
var logContext = new JObject();
foreach (var kvp in dict)
    {
    logContext?.Add(kvp.Key, kvp.Value as JToken ?? new JValue(kvp.Value));
    }

I even tried "forcing" the conversion:
C#
logContext?.Add(kvp.Key, (kvp.Value as JToken) ?? new JValue(kvp.Value));
but it made no difference.
So ... we can't help you directly.
I'd start by changing all the var definitions to explicit types, to be sure of what I was working with (VS will do that for you) then break out your single line of code into multiple statements to try and see what part of it is doesn't like.

Sorry - but if we can't reproduce it, we can't solve it!
   
Comments
bholey 5-May-21 6:53am
   
thanks Griff for quick help and suggestion! giving more details -
actually, above code block working properly in application but reported as logically dead code issue in Polaris security scan report so thinking what missed as per coding standard and what to change.
BillWoodruff 6-May-21 1:07am
   
+5 unwarranted vote of #3 countered
logContext?.Add(kvp.Key, kvp.Value as JToken ?? new JValue(kvp.Value));

removing of ? null check from logContext?, resolved security risk vulnerabilities
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900