【问题标题】:DeserializeObject With Static Class带静态类的反序列化对象
【发布时间】:2020-09-04 20:54:52
【问题描述】:

我想用 json 设置文件转换我的静态类的反序列化。

public class Welcome
{
    public ConfigurationProvider ConfigurationProvider { get; set; }
}
[JsonObject(MemberSerialization.OptIn)]
public class ConfigurationProvider
{
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
    public static AppConfig Rcom { get;  }
    [JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
    public static AppConfig Ccst { get;  }
    
    static ConfigurationProvider()
    {
        Rcom=new AppConfig("RCOM");
        Ccst=new AppConfig("CCST");
    }
    

}

public class AppConfig
{
    public GarConfiguration GarConfiguration { get;  }
    public VacationRentalRedisKeysConfiguration VacationRentalRedisKeysConfiguration { get;  }

    public AppConfig(string type)
    {
        if (type == "RCOM")
        {
            GarConfiguration = new GarConfiguration(type);
            VacationRentalRedisKeysConfiguration = new VacationRentalRedisKeysConfiguration
            {
                DefaultVacationRentalThemeKey = "RcomKey",
                 CheckVacationRentalThemeKey=true
            };
        }
        else
        {
            GarConfiguration = new GarConfiguration(type);
            VacationRentalRedisKeysConfiguration = new VacationRentalRedisKeysConfiguration
            {
                DefaultVacationRentalThemeKey = "CCSTKey",
                CheckVacationRentalThemeKey=false
            };
        }
       
    }
}
public class GarConfiguration
{
    public GarSearch GarSearch { get; }

    public GarConfiguration(string type)
    {
        if (type == "RCOM")
        {
            GarSearch = new GarSearch
            {
                RequestSplitedMinCount = 40,
                RequestSplitedCount = 5,
                RequestSplitedMaxTimeOutInMilliSecond = 3000,
                RequiredSplitedRequest = true,
                RequiredSplitedRequestMaxTimeOut = true,
                AppName = type
            };
        }
        else
        {
            GarSearch = new GarSearch
            {
                RequestSplitedMinCount = 40,
                RequestSplitedCount = 5,
                RequestSplitedMaxTimeOutInMilliSecond = 5000,
                RequiredSplitedRequest = true,
                RequiredSplitedRequestMaxTimeOut = true,
                AppName = type
            };
        }
    }
}
public class GarSearch
{
    public long RequestSplitedMinCount { get; set; }
    public long RequestSplitedCount { get; set; }
    public long RequestSplitedMaxTimeOutInMilliSecond { get; set; }
    public bool RequiredSplitedRequest { get; set; }
    public bool RequiredSplitedRequestMaxTimeOut { get; set; }
    public string AppName { get; set; }
}


public class VacationRentalRedisKeysConfiguration
{
    public bool CheckVacationRentalThemeKey { get; set; }
    public string DefaultVacationRentalThemeKey { get; set; }
}

上面是我的示例类。

我需要这个配置,因为如果我没有得到 JSON 设置文件,那么它将从这个默认设置运行,否则它将从那个 JSON 设置文件运行。

我需要这个静态的,因为不想为此创建多个实例。

当我反序列化我的课程时,我会得到类似的结果 class Deserialize proeprly but not able to access static member Not able to get static memeber values

【问题讨论】:

  • 您应该将编程语言添加为标签
  • '我需要这个静态,因为不想为此创建多个实例':不合理。您必须在static 和可序列化之间进行选择。

标签: c# json serialization static-methods


【解决方案1】:

我可能误解了你的问题,因为你的标题是“DeserializeObject With Static Class”,但正文却是“Deserialize my static class”

如果您打算反序列化静态类,则不能反序列化或序列化静态类,因为如果您想了解更多说明,则永远不会有任何静态类实例去here

如果您的意思是要将对象转换为 JSON 字符串(可以使用下面的静态方法)

using Newtonsoft.Json;

        public static string ConvertObjectToJson<T>(T Ls)
        {
            if (Ls == null)
                return null;
            return JsonConvert.SerializeObject(Ls);


        }

如果您的意思是要将 JSON 字符串转换为对象(可以使用下面的静态方法)

        public static T JasonConvertToObj<T>(string JsonStr , T FalseReturn)
        {
            try
            {
                return JsonConvert.DeserializeObject<T>(JsonStr);
            }
            catch (Exception e)
            {
                string erorstr = e.Message;
                return FalseReturn;
            }
        }

如果我仍然没有回答您的问题,请告诉我。如果我的代码中有错误,我想知道,但如果我没记错的话,我使用了它。

【讨论】:

    猜你喜欢
    • 2020-06-07
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多