1

ソケットで HttpWebRequest を送信しようとしましたが、それをシリアライズしたいときに、次の例外が発生します。

タイプ 'System.Runtime.Serialization.SerializationException' の未処理の例外が mscorlib.dll で発生しました' はシリアライズ可能としてマークされていません。

これは私のコードです:

 [Serializable()]

class BROWSER
{
    HttpListener Response = null;
    HttpListenerContext context = null;
    HttpListenerRequest request = null;
    HttpListenerResponse response = null;
    public HttpWebRequest Webrequest = null;
    public WebResponse Webresponce = null;
    string responseString = null; 
    //when my Webrequest is ready ...
      public void SendToProxyServer(HttpWebRequest Webrequest)
    {
        byte[] SndData;
        S_RSocket sock = new S_RSocket();
        SndData = Serializ.seryaliz(this.Webrequest);
        sock.Send(SndData);
        this.responseString = sock.res;

    }

および私のシリアライズクラス:

abstract class Serializ
 {
   #region Filds
   static MemoryStream ms = null;
   static BinaryFormatter formatter = null;
   #endregion
   #region Methods
   public static byte [] seryaliz(object obj)
   {
       ms = new MemoryStream();
       formatter = new BinaryFormatter();
       formatter.Serialize(ms, obj);
       ms.Position = 0;
       String tmp = null;
       StreamReader sr = new StreamReader(ms);
       tmp = sr.ReadToEnd();
       ms.Position = 0;
       sr.Close();
       ms.Close();
       return Encoding.ASCII.GetBytes(tmp);
   }
   public static object DEseryaliz(byte [] Data)
   {
       object obj = new object();
       formatter = new BinaryFormatter();
       ms = new MemoryStream(Data);
       ms.Position = 0;
       obj=(object)formatter.Deserialize(ms);
       ms.Close();
       return obj;

   }
   #endregion 
 }

クラスを Serializable() として設定していますが、機能していませんでした。

問題はどこだ?

4

2 に答える 2

2

HttpWebRequest をシリアル化することはできませんSerializable。属性がないためにシリアル化できないためです。この場合、HttpWebRequest 内のプロパティです。シリアライズを行っているクラスに追加Serializableしても何も起こりません。

于 2015-02-03T22:39:04.160 に答える
2

問題はどこですか?

問題は例外で完全に説明されています。

アセンブリ 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' の 'System.Net.WebRequest+WebProxyWrapper' と入力すると、シリアル化可能としてマークされません。

何かをマークしたからといって、それが実際にSerializableAttributeごとに可能であることを[Serializable()]意味するわけではありません。

SerializableAttribute 属性を型に適用して、この型のインスタンスをシリアル化できることを示します。シリアル化されているオブジェクトのグラフ内のいずれかの型に SerializableAttribute 属性が適用されていない場合、共通言語ランタイムは SerializationException をスローします。

フィールドまたはプロパティの 1 つが、シリアル化可能としてマークされていないWebRequestクラスを内部的に持つを使用しています。WebProxyWrapperしたがって、そのフィールドまたはプロパティの 1 つが を使用しているため、WebRequest をシリアル化できませんWebProxyWrapper

于 2015-02-03T22:39:52.577 に答える