Exception while deserializing DataTable with custom DataType columns using BinaryFormatter

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?

-------------Problems Reply------------

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

public class Struct
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?

Category:c# Views:1 Time:2016-03-13

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.157 (s). 11 q(s)