.net动态字符串,如果条件

时间:2020-07-03 10:19:02

标签: .net json

我想在条件中使用动态字符串,但代码却出现异常。

   var params = {};
   params.windowKey = "File";

此params变量传递另一个函数供以后使用,如下所示: escape(Ext.encode(params))

下面的代码有问题。

  dynamic allFields = JSON.Deserialize(Ext.Net.Utilities.EscapeUtils.Unescape(jsonVariable));

  if (allFields.windowKey.Value = "File")
  {
      UploadDocument(data.FileName, fileBytes, allFields.sqId);
  }
  else if (allFields.windowKey.Value = "Csv")
  {
      UploadCSV(sender, data);
  }

如果代码出现在if blok中,它将获得异常。 我该如何控制价值?

1 个答案:

答案 0 :(得分:1)

您不需要.Value来检索值。可以将dynamic视为基于字符串的字典。

var json = @"{""windowKey"": ""File""}";
dynamic allFields = //Deserialize(json);

//access the properties by their name
Console.WriteLine(allFields.windowKey); //prints: File
Console.WriteLine(allFields["windowKey"]); //prints: File

if (allFields.windowKey == "File") {
    //do something
}
相关问题