【问题标题】:How to transform this code ob block to a function?如何将此代码 ob 块转换为函数?
【发布时间】:2020-11-08 07:09:03
【问题描述】:

我在我的代码中经常使用下面的代码块。我想把它放在一个函数中,但它不起作用。

using (StreamReader streamReader = new StreamReader(path, true))
                {
                    string json = streamReader.ReadToEnd();
                    userdata = JsonConvert.DeserializeObject<List<User>>(json);
                    streamReader.Close();
                }


//List<User> userdata = new List<User>();//this is a comment 

我正在反序列化一个包含对象数组的 json 文件。

这是我写的函数。

 static void deserializeuser(List<dynamic> @params, string y)
    {
        using (StreamReader streamReader = new StreamReader(y, true))
        {
            string json = streamReader.ReadToEnd();
            @params = JsonConvert.DeserializeObject<List<dynamic>>(json);
            streamReader.Close();
        }

    }

这个函数不显示错误但是我不知道怎么调用它。

【问题讨论】:

  • 你试过了吗?你能展示你尝试了什么吗?
  • “但代码不起作用”不是很具体。您能否请edit您的问题让我们知道预期结果是什么以及您的代码如何未能实现它? Reading this 可能有用

标签: c# json list


【解决方案1】:

试试

    public List<T> GetFromPath<T>(string path)
    {
        List<T> ret = null;
        try
        {
            using (StreamReader streamReader = new StreamReader(path, true))
            {
                string json = streamReader.ReadToEnd();
                ret = JsonConvert.DeserializeObject<List<T>>(json);
                streamReader.Close();
            }
        }
        catch(Exception ex)
        {
            // Log here
            throw;
        }
        return ret;
    }

或

    public List<T> GetFromPath<T>(string path)
    {
        List<T> ret = null;
        try
        {
            if(File.Exists(path))
                ret = JsonConvert.DeserializeObject<List<T>>(File.ReadAllText(path));
        }
        catch(Exception ex)
        {
            // Log here
            throw;
        }
        return ret;
    }

编辑

将其设为通用调用,使用

List<User> list = GetFromPath<User>("somepath");

【讨论】:

    【解决方案2】:

    这应该可行:

    private static List<T> DeserializeList<T>(string path)
    {
        List<T> result = null;
    
        using (var streamReader = new StreamReader(path, true))
        {
            string json = streamReader.ReadToEnd();
            result = JsonConvert.DeserializeObject<List<T>>(json);
            streamReader.Close();
        }
    
        return result;
     }
    

    ...

    List<User> userdata = DeserializeList<User>(path);
    

    【讨论】:

      【解决方案3】:

      更通用的函数风格

      private static T GetTypedDataFromFile<T>(string path)
      {
          T result = null;
          // I prefer C# 8.0 using syntax here.
          // If your C# version is older, please revert to original with brackets
          using var streamReader = new StreamReader(path, true);
          
          string json = streamReader.ReadToEnd();
          result = JsonConvert.DeserializeObject<T>(json);
          streamReader.Close();
          
          return result;
       }
      

      然后调用

      List<User> users = DeserializeList<List<User>>(path);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-05
        • 1970-01-01
        • 1970-01-01
        • 2021-08-01
        • 2010-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多