C#Class Properties'并列#39;到文件? (见Perl :: Tie)

时间:2017-06-24 05:35:38

标签: c# perl data-structures persistent tie

在perl中,有一种将数据结构绑定到文件的相当简单的方法,无论是字符串,散列(C#中的字典)还是简单的数组/列表。

我在C#中拼凑了我自己的半解决方案,但我想知道是否有更内置的方式来实现此功能?

编辑以回应下面的评论 - 问题,直接:是否有内置的方式来" tie"一个文件的类/字典,所以任何一个的变化都反映在另一个文件中? (没有按照我在下面所做的那样做)

(绑定字典意味着字典中的任何更改都会立即在文件中反映/更新,并声明绑定的var会将对象从磁盘加载(如果已存在);请参阅PerlTie

我的伪领带课程如下:

    #region options
    private float _Opacity;
    public float Opacity {
        get {
            return Opacity;
            }
        set {
            _Opacity = value;
            this.Save();
            }
        }

    private Font _Font;
    public Font Font {
        get {
            return _Font;
            }
        set {
            _Font = value;
            this.Save();
            }
        }

    private Color _FontColor;
    public Color FontColor {
        get {
            return _FontColor;
            }
        set {
            _FontColor = value;
            this.Save();
            }
        }

    private Color _BGColor;
    public Color BGColor {
        get {
            return _BGColor;
            }
        set {
            _BGColor = value;
            this.Save();
            }
        }

    private Point _Location;
    public Point Location {
        get {
            return _Location;
            }
        set {
            _Location = value;
            this.Save();
            }
        }

    private Size _Size;
    public Size Size {
        get {
            return _Size;
            }
        set {
            _Size = value;
            this.Save();
            }
        }

    private ushort _HistoryLines;
    public ushort HistoryLines {
        get {
            return _HistoryLines;
            }
        set {
            _HistoryLines = value;
            this.Save();
            }
        }

    private ChatType _ChatModes;
    public ChatType ChatModes {
        get {
            return _ChatModes;
            }
        set {
            _ChatModes = value;
            this.Save();
            }
        }

    private bool _Debugging;
    public bool Debugging {
        get {
            return _Debugging;
            }
        set {
            _Debugging = value;
            this.Save();
            }
        }
    #endregion options

    private FontConverter FontConvert;
    private FileInfo SettingsFile;

    private MLogConf() {
        }

    public MLogConf(string stateFile) {
        FontConvert = new FontConverter();
        try {

            if (!Directory.Exists(Path.GetDirectoryName(stateFile)))
                Directory.CreateDirectory(Path.GetDirectoryName(stateFile));
            if (!File.Exists(stateFile)) {
                FileStream fs = File.Create(stateFile);
                fs.Close();
                }
            SettingsFile = new FileInfo(stateFile);
            if (SettingsFile.Length == 0) {
                this.SetDefaultOptions();
                } else {
                if (!this.Load()) {
                    throw new FileLoadException("Couldn't load settings file");
                    }
                }
            } catch (Exception ex) {
            Trace.Write($"Failed to create MLogConf({nameof(stateFile)}) {ex.Message + Environment.NewLine + ex.StackTrace}");
            }
        }

    private bool Load() {
        if (SettingsFile == null)
            return false;
        try {
            byte[] data = File.ReadAllBytes(SettingsFile.FullName);
            using (MemoryStream m = new MemoryStream(data)) {
                using (BinaryReader reader = new BinaryReader(m)) {
                    _Opacity = reader.ReadSingle();
                    _Font = (Font)(FontConvert.ConvertFromString(Encoding.ASCII.GetString(Convert.FromBase64String(reader.ReadString()))));
                    _FontColor = Color.FromArgb(reader.ReadInt32());
                    _BGColor = Color.FromArgb(reader.ReadInt32());
                    _Location = new Point(reader.ReadInt32(), reader.ReadInt32());
                    _Size = new Size(reader.ReadInt32(), reader.ReadInt32());
                    _HistoryLines = reader.ReadUInt16();
                    _ChatModes = (ChatType)reader.ReadInt32();
                    _Debugging = reader.ReadBoolean();
                    }
                }
            } catch (Exception e) {
            Trace.WriteLine($"Exception reading binary data: {e.Message + Environment.NewLine + e.StackTrace}");
            return false;
            }
        return true;
        }

    private bool Save() {
        try {
            using (FileStream fs = new FileStream(SettingsFile.FullName, FileMode.Create)) {
                using (BinaryWriter writer = new BinaryWriter(fs)) {
                    writer.Write(_Opacity);
                    writer.Write(Convert.ToBase64String(Encoding.ASCII.GetBytes((string)FontConvert.ConvertTo(Font, typeof(string)))));
                    writer.Write(_FontColor.ToArgb());
                    writer.Write(_BGColor.ToArgb());
                    writer.Write(_Location.X);
                    writer.Write(_Location.Y);
                    writer.Write(_Size.Width);
                    writer.Write(_Size.Height);
                    writer.Write(_HistoryLines);
                    writer.Write((int)_ChatModes);
                    writer.Write(_Debugging);
                    }
                }
            } catch (Exception e) {
            Trace.WriteLine($"Exception writing binary data: {e.Message + Environment.NewLine + e.StackTrace}");
            return false;
            }
        return true;
        }


    private bool SetDefaultOptions() {
        this._BGColor = Color.Black;
        this._ChatModes = ChatType.Alliance | ChatType.Emote | ChatType.FreeCompany | ChatType.Linkshell | ChatType.Party | ChatType.SayShoutYell | ChatType.Tell;
        this._Opacity = 1f;
        this._Font = new Font("Verdana", 50);
        this._FontColor = Color.CornflowerBlue;
        this._Location = new Point(100, 400);
        this._Size = new Size(470, 150);
        this._HistoryLines = 512;
        this._Debugging = false;
        return this.Save();
        }

1 个答案:

答案 0 :(得分:1)

我想你正在寻找一种在文件中存储类实例的简单方法。

序列化是将对象转换为字节流以便存储对象或将其传输到内存,数据库或文件的过程。其主要目的是保存对象的状态,以便能够在需要时重新创建它。反向过程称为反序列化。

我前段时间为C#here找到的最短解决方案是来自Newtonsoft的Json.NET,可以作为NuGet包使用。它将为您完成所有“长类属性到字符串代码”,并为您准备好要写入文件的字符串。 官方网站可以提供代码示例:http://www.newtonsoft.com/json

保存到文件:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }

从文件中读取:

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys