0

Few days ago I've asked a question about object serialization and the answer was working well, actually it still works. But somehow I've copied the serialize function to an another class but didn't fit here and I have no idea why.

Exception has been thrown by the target of an invocation.

Inner Exception: An invalid argument was supplied.

Client.cs

public class Client
{
    private string username;
    public string Username
    {
        get { return username; }
        set { username = value; }
    }
    private TcpClient tclient;
    public TcpClient tClient
    {
        get { return tclient; }
        set { tclient = value; }
    }
    public Client()
    {}

    public string Serialize(object obj)
    {
        var serializer = new JavaScriptSerializer();
        return serializer.Serialize(obj);
    }
    public object Deserialize(string json)
    {
        var serializer = new JavaScriptSerializer();
        return serializer.Deserialize<object>(json);
    }
}

Any ideas?

Community
  • 1
  • 1
osumatu
  • 410
  • 1
  • 8
  • 25

1 Answers1

0

Since the TcpClient class is not a serializable class you can't simply serialize it. But as you can see here it has a constructor with (string: server, int: port) so you can add these fields to your class then after deserialization you can create it again.

Ps: It's funny that I was the one answered your previous question, here we are again.

Sahin
  • 114
  • 7