示例#1
0
        public static bool TryWrite <T>(T model, out string message, string filepath) where T : class, new()
        {
            bool b = false;

            message = string.Empty;
            //filepath = !String.IsNullOrWhiteSpace(filepath) ? filepath : Filepath<T>();
            try
            {
                //JsonSerializerOptions options = new JsonSerializerOptions()
                //{
                //    WriteIndented = true,
                //    IgnoreReadOnlyProperties = true,
                //    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
                //};

                //var json = JsonConvert.SerializeObject(model, Formatting.Indented);
                //File.WriteAllText(filepath, json);
                GenericObjectManager.WriteGenericItem <T>(model, filepath);
                b = true;
            }
            catch (Exception ex)
            {
                message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            return(b);
        }
示例#2
0
        public static bool TryRead <T>(string filepath,
                                       out List <T> list,
                                       out string message)
        {
            bool b = false;

            list    = null;
            message = string.Empty;
            try
            {
                if (File.Exists(filepath))
                {
                    //string filecontents = File.ReadAllText(filepath);
                    //if (!string.IsNullOrWhiteSpace(filecontents))
                    //{
                    //    list = JsonConvert.DeserializeObject<List<T>>(filecontents);

                    //    b = true;
                    //}
                    list = GenericObjectManager.ReadGenericList <T>(filepath);
                    b    = true;
                }
            }
            catch (Exception ex)
            {
                message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }

            return(b);
        }
示例#3
0
        public static bool TryRead <T>(out List <T> list,
                                       out string message,
                                       string filepath) where T : class, new()
        {
            bool b = false;

            message = string.Empty;
            list    = new List <T>();
            //filepath = !String.IsNullOrWhiteSpace(filepath) ? filepath : Filepath<T>();
            try
            {
                if (File.Exists(filepath))
                {
                    list = GenericObjectManager.ReadGenericList <T>(filepath);
                    //string filecontents = File.ReadAllText(filepath);
                    //if (!string.IsNullOrWhiteSpace(filecontents))
                    //{

                    //    list = JsonConvert.DeserializeObject<List<T>>(filecontents);
                    //}
                    b = list.Count > 0;
                }
            }
            catch (Exception ex)
            {
                message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
            }
            return(b);
        }