将模型类作为参数传递给函数

时间:2020-04-15 19:05:42

标签: c#

我可以使用以下代码将用户设置保存在json文件中

internal class GlobalData
    {
        public static void Init()
        {
            if (File.Exists(AppConfig.SavePath))
            {
                try
                {
                    string json = File.ReadAllText(AppConfig.SavePath);
                    Config = (string.IsNullOrEmpty(json) ? new AppConfig() : JsonConvert.DeserializeObject<AppConfig>(json)) ?? new AppConfig();

                }
                catch
                {
                    Config = new AppConfig();
                }
            }
            else
            {
                Config = new AppConfig();
            }
        }

        public static void Save()
        {
            string json = JsonConvert.SerializeObject(Config);
            File.WriteAllText(AppConfig.SavePath, json);
        }

        public static AppConfig Config { get; set; }

    }

这是模特

internal class AppConfig
    {
        public static readonly string SavePath = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\AppConfig.json";

        public int LastApi { get; set; } = 1545;
        public string IMEI { get; set; }
        public string Authorization { get; set; }
    }

我想将此GlobalData.cs类制作为库 但是,正如您所看到的,在GlobalData类中定义的模型类,我的问题是如何将模型类(AppConfig.cs)作为参数传递给GlobalData类,以便用户可以创建具有任何属性的任何模型类? / p>

0 个答案:

没有答案