0

我想将 FileSystemWatcher 对象存储在 sqlite 数据库中以供将来参考,但是在序列化对象时出现以下异常

“在程序集 'System、Version=2.0.0.0、Culture=neutral、PublicKeyToken=b77a5c561934e089' 中键入 'System.IO.FileSystemWatcher' 未标记为可序列化。”

这是代码

            MemoryStream memorystream = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(memorystream, FSWatcher);
            byte[] yourBytesToDb = memorystream.ToArray();

谢谢和问候, 哈西娜

4

1 回答 1

0

使用 XML 之一

public static void SaveAsXML(Object A, string FileName)
{
    var serializer = new XmlSerializer(A.GetType());
    using (var textWriter = new StreamWriter(FileName))
    {
        serializer.Serialize(textWriter, A);
        textWriter.Close();
    }
}

public static T LoadFromXML<T>(string FileName) where T : class
{
    if (File.Exists(FileName))
    {
        using (var textReader = new StreamReader(FileName))
        {
            var deserializer = new XmlSerializer(typeof(T));
            return (T)(deserializer.Deserialize(textReader));
        }
    }
    return default(T);
}
于 2013-08-05T09:09:28.793 回答