0

I'm writing a client-server application. I want to send DataTable table where most of columns are of type Pair. I have that public class Pair inside public class Struct on both Server and Client.

[Serializable]
public class Struct
{
    public class Pair
    {
        public int a { get; set; }
        public int b { get; set; }
            ...
        public override string ToString()
        {
            return this.a.ToString() + " " + this.b.ToString();
        }
    }
        ...
}

I send it from server:

(new BinaryFormatter()).Serialize(nStream, table);

Accept on client:

DataTable table = (DataTable)(new BinaryFormatter()).Deserialize(nStream);

And here I get a

TargetInvocationException "Exception has been thrown by the target of an invocation" with InnerException: ArgumentException "Column requires a valid DataType".

How to send this table over network and deserialize it?

dbc
  • 104,963
  • 20
  • 228
  • 340

1 Answers1

0

You need to mark the inner class as [Serializable] also:

[Serializable]
public class Struct
{
    [Serializable]
    public class Pair
    {
        public int a { get; set; }
        public int b { get; set; }
    }
}

A nested class is not a subclass, it's an independent type that happens to have access to the protected and private members of its containing type.

Also, are you actually linking the same assembly that contains your Struct in both the client and server, or did you just copy the code? BinaryFormatter actually records the assembly name and version of the types being serialized, and if, upon deserialization, that specific assembly is not found, deserialization will fail. See How to serialize/deserialize an object loaded from another assembly?

dbc
  • 104,963
  • 20
  • 228
  • 340