首页 > 解决方案 > If条件下的.net动态字符串

问题描述

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

   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 块时,它会出现异常。我如何控制价值?

标签: .netjson

解决方案


您不需要.Value检索该值。将动态视为基于字符串的字典。

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
}

推荐阅读