4

オブジェクトグラフを文字列にシリアル化してから、文字列から逆シリアル化しようとしています。これを行うと、オブジェクトは問題なくシリアル化されます

using (var memStream = new System.IO.MemoryStream())
{
     mf.Serialize(memStream, this);
     memStream.Seek(0, 0);

     Search s;
     using (var memStrClone = new System.IO.MemoryStream())
     {
          memStream.CopyTo(memStrClone);
          memStrClone.Seek(0, 0);
          s = mf.Deserialize(memStrClone) as Search;
     }
}

上記のコードは機能しますが、文字列にシリアル化し、このように同じ文字列を逆シリアル化しようとします

Search s;
string xml = ToString<Search>(this);
s = FromString<Search>(xml);

public static TType FromString<TType>(string input)
{
     var byteArray = Encoding.ASCII.GetBytes(input);
     using (var stream = new MemoryStream(byteArray))
     {
          var bf = new BinaryFormatter();
          return (TType)bf.Deserialize(stream);
     }
}

public static string ToString<TType>(TType data)
{
     using (var ms = new MemoryStream())
     {
          var bf = new BinaryFormatter();
          bf.Serialize(ms, data);
          return Encoding.ASCII.GetString(ms.GetBuffer());
     }
}

例外をスローします

オブジェクトタイプ「1936026741Core.Sebring.BusinessObjects.Search.Search」のアセンブリIDがありません。

どんな助けでも大歓迎です。ありがとう。

4

1 に答える 1

8

ここにあなたがしたいことをするコードがあります(私は思います)-しかし、私は尋ねなければなりません-なぜそのような文字列にシリアライズしたいのですか?

クラスが単純で文字列にシリアル化できる場合は、はるかに扱いやすい XML シリアライザーを使用してください。ディスクにシリアル化する場合は、バイナリをファイルに書き込みます。複雑で、シリアル化して送信する場合は、protobuf-net などの使用を検討してください。

あなたの問題の核心は、ASCIIエンコーディングを使用しようとしていることだと思います-私はBase64エンコーディングを使用しています。

とにかく-ここに行きます(私はあなたの検索クラスを推測しました!)

 class Program
{
    [Serializable]
    public class Search
    {
        public Guid ID { get; private set; }

        public Search() { }

        public Search(Guid id)
        {
            ID = id;
        }

        public override string ToString()
        {
            return ID.ToString();
        }
    }

    static void Main(string[] args)
    {
        Search search = new Search(Guid.NewGuid());
        Console.WriteLine(search);
        string serialized = SerializeTest.SerializeToString(search);
        Search rehydrated = SerializeTest.DeSerializeFromString<Search>(serialized);
        Console.WriteLine(rehydrated);

        Console.ReadLine();
    }
}

public class SerializeTest
{
    public static Encoding _Encoding = Encoding.Unicode;

    public static string SerializeToString(object obj)
    {
        byte[] byteArray = BinarySerializeObject(obj);
        return Convert.ToBase64String(byteArray);
    }

    public static T DeSerializeFromString<T>(string input)
    {
        byte[] byteArray = Convert.FromBase64String(input);
        return BinaryDeserializeObject<T>(byteArray);
    }

    /// <summary>
    /// Takes a byte array and deserializes it back to its type of <see cref="T"/>
    /// </summary>
    /// <typeparam name="T">The Type to deserialize to</typeparam>
    /// <param name="serializedType">The object as a byte array</param>
    /// <returns>The deserialized type</returns>
    public static T BinaryDeserializeObject<T>(byte[] serializedType)
    {
        if (serializedType == null)
            throw new ArgumentNullException("serializedType");

        if (serializedType.Length.Equals(0))
            throw new ArgumentException("serializedType");

        T deserializedObject;

        using (MemoryStream memoryStream = new MemoryStream(serializedType))
        {
            BinaryFormatter deserializer = new BinaryFormatter();
            deserializedObject = (T)deserializer.Deserialize(memoryStream);
        }

        return deserializedObject;
    }

    /// <summary>
    /// Takes an object and serializes it into a byte array
    /// </summary>
    /// <param name="objectToSerialize">The object to serialize</param>
    /// <returns>The object as a <see cref="byte"/> array</returns>
    public static byte[] BinarySerializeObject(object objectToSerialize)
    {
        if (objectToSerialize == null)
            throw new ArgumentNullException("objectToSerialize");

        byte[] serializedObject;

        using (MemoryStream stream = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, objectToSerialize);
            serializedObject = stream.ToArray();
        }

        return serializedObject;
    }

}

HTH

于 2013-02-16T21:04:04.753 に答える