コード例 #1
0
 public static ISupportSerialization FromFile(string fileName, Type t)
 {
     if (string.IsNullOrEmpty(fileName))
     {
         return(null);
     }
     if (!File.Exists(fileName))
     {
         return(null);
     }
     try {
         ISupportSerialization obj       = null;
         XmlSerializer         formatter = new XmlSerializer(t);
         using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate)) {
             obj = (ISupportSerialization)formatter.Deserialize(fs);
         }
         obj.FileName = fileName;
         obj.OnEndDeserialize();
         return(obj);
     }
     catch (Exception e) {
         Telemetry.Default.TrackException(e);
         return(null);
     }
 }
コード例 #2
0
        public static bool Save(ISupportSerialization obj, Type t, string path)
        {
            if (File.Exists(obj.FileName))
            {
                obj.FileName = Path.GetFileName(obj.FileName);
            }
            string fullName = string.IsNullOrEmpty(path)? obj.FileName: path + "\\" + obj.FileName;
            string tmpFile  = fullName + ".tmp";

            if (!string.IsNullOrEmpty(path) && !Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            if (string.IsNullOrEmpty(obj.FileName))
            {
                return(false);
            }
            try {
                XmlSerializer formatter = new XmlSerializer(t);
                using (FileStream fs = new FileStream(tmpFile, FileMode.Create)) {
                    formatter.Serialize(fs, obj);
                }
                if (File.Exists(fullName))
                {
                    File.Delete(fullName);
                }
                File.Move(tmpFile, fullName);
            }
            catch (Exception e) {
                Telemetry.Default.TrackException(e);
                return(false);
            }

            return(true);
        }
コード例 #3
0
        public static bool Load(ISupportSerialization obj, Type t)
        {
            string fileName = obj.FileName;
            object res      = FromFile(fileName, t);

            if (res == null)
            {
                return(false);
            }

            PropertyInfo[] props = obj.GetType().GetProperties(System.Reflection.BindingFlags.Public | BindingFlags.Instance);
            foreach (var prop in props)
            {
                if (!prop.CanRead)
                {
                    continue;
                }
                if (prop.GetCustomAttribute(typeof(XmlIgnoreAttribute)) != null)
                {
                    continue;
                }
                if (prop.CanWrite)
                {
                    prop.SetValue(obj, prop.GetValue(res, null));
                }
                else
                {
                    if (prop.PropertyType.IsValueType)
                    {
                        continue;
                    }
                    object value = prop.GetValue(res, null);
                    if (value is IList)
                    {
                        IList srcList = (IList)value;
                        IList dstList = (IList)prop.GetValue(obj, null);
                        dstList.Clear();
                        for (int i = 0; i < srcList.Count; i++)
                        {
                            dstList.Add(srcList[i]);
                        }
                    }
                    else if (value is IDictionary)
                    {
                        IDictionary srcDict = (IDictionary)value;
                        IDictionary dstDict = (IDictionary)prop.GetValue(obj, null);
                        dstDict.Clear();
                        foreach (object key in srcDict.Keys)
                        {
                            dstDict.Add(key, srcDict[key]);
                        }
                    }
                }
            }
            return(true);
        }