0

文字列とハッシュテーブルを持つクラスがあります。ハッシュテーブルには、キー(文字列)値のセットと各キー属性のビットマップファイルが含まれています。これをバイナリファイルにシリアル化するにはどうすればよいですか?

    public void SerializeObject(List<Poem> poems)
    {

        using (Stream stream = File.Open("data.bin", FileMode.Create))
        {
            BinaryFormatter bin = new BinaryFormatter();
            bin.Serialize(stream, poems);
        }
    }

    public List<Poem> DeSerializeObject()
    {
        List<Poem> poems1;
        using (Stream stream = File.Open("data.bin", FileMode.Open))
        {
            BinaryFormatter bin = new BinaryFormatter();

            var lizards2 = (List<Poem>)bin.Deserialize(stream);
            poems1 = (List<Poem>)lizards2;
        }
        return poems1;
    }

//詩クラス

   [Serializable()]
public class Poem  
{
    string poemName;
    Hashtable poemContent; contains set of keys(strings) , values(bitmap)//

    public Poem() {

        poemContent = new Hashtable();

    }
    public string PoemName
    {
        get { return poemName; }
        set { poemName = value; }
    }

    public Hashtable PoemContent
    {
        get { return poemContent; }
        set { poemContent = value; }
    }}

しかし、これは常にエラーを生成します。

4

1 に答える 1

1

エラーなしでコードを実行できます。呼び出しコード:

   SerializeObject(new List<Poem>
                                {
                                    new Poem
                                        {
                                            PoemContent = new Hashtable {{"Tag", new System.Drawing.Bitmap(1, 1)}},
                                            PoemName = "Name"
                                        }
                                });

 var poems2 = DeserializeObject();

表示されているエラーは何ですか? コンパイラ エラーですか、それとも実行時例外ですか? このコードは、詩のリストの例で問題なく実行できます。ちなみに、Dictionary<K,V>代わりに使用することをお勧めしHashTableます。

于 2012-10-26T12:52:53.723 に答える