BinaryFormatter.Deserialize

  • Thread starter Thread starter Igor
  • Start date Start date
I

Igor

Hi.

While executing BinaryFormatter.Deserialize() I get:
System.InvalidCastException: Specified cast is not valid.
I implemented ISerializable interface. What may be a problem?

Thanks.
 
[Serializable()]

public class ApplicationInfo:IDisposable,ISerializable

{

private Guid session; // 16 bytes

private Guid sessionNW; // 16 bytes

private string ANI;

private string DNIS;

private string mqNW; // networking queue

private string parent;

private string child;

private string applicationQueue;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo()

{

}

public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)

{

session = (Guid)info.GetValue("session", typeof(Guid));

sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));

ANI = (string)info.GetValue("ANI", typeof(string));

DNIS = (string)info.GetValue("DNIS", typeof(string));

mqNW = (string)info.GetValue("mqNW", typeof(string));

parent = (string)info.GetValue("parent", typeof(string));

child = (string)info.GetValue("child", typeof(string));

applicationQueue = (string)info.GetValue("applicationQueue",
typeof(string));

applicationName = (string)info.GetValue("applicationName", typeof(string));

machineID = (int)info.GetValue("machineID", typeof(int));

boardID = (int)info.GetValue("boardID", typeof(int));

channelID = (int)info.GetValue("channelID", typeof(int));

}

public ApplicationInfo(string session, string sessionNW, string ANI, string
DNIS, string mqNW, string parent, string child, string applicationQueue, int
machine, int board, int channel, string applicationName)

{

this.session = new Guid(session);

this.sessionNW = new Guid(sessionNW);

this.ANI = ANI;

this.DNIS = DNIS;

this.mqNW = mqNW;

this.parent = parent;

this.child = child;

this.applicationQueue = applicationQueue;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicationName = applicationName;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo.Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext context)

{

info.AddValue("session", session);

info.AddValue("sessionNW", sessionNW);

info.AddValue("ANI", ANI);

info.AddValue("DNIS", DNIS);

info.AddValue("mqNW", mqNW);

info.AddValue("parent", parent);

info.AddValue("child", child);

info.AddValue("applicationQueue", applicationQueue);

info.AddValue("machineID", machineID);

info.AddValue("boardID", boardID);

info.AddValue("channelID", channelID);

info.AddValue("applicationName", applicationName);

}

#endregion

}

..........

ApplicationInfo app;

MemoryStream stream = new MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, app);



MessageQueue mq = new MessageQueue(mqPath);

Message m = new Message(stream, new BinaryMessageFormatter());

mq.Formatter = new BinaryMessageFormatter();

mq.Send(m);



.........



public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)source;

Message m = mq.EndReceive(asyncResult.AsyncResult);



m.Formatter = new BinaryMessageFormatter();

incomingMessage(source, new IncomingMessageEventArgs((object)m.BodyStream,
false));

mq.BeginReceive();

}

catch(Exception ex)

{

Console.WriteLine(ex.ToString());

}

}



......



public void OnIncomingMessage(object source, IncomingMessageEventArgs e)

{

BinaryFormatter formatter = new BinaryFormatter();

application = null;

application = (ApplicationInfo)formatter.Deserialize((Stream)e.getDocument);

}
 
[Serializable()]
public class ApplicationInfo:IDisposable,ISerializable
{
private Guid session; // 16 bytes
private Guid sessionNW; // 16 bytes
private string ANI;
private string DNIS;
private string mqNW; // networking queue
private string parent;
private string child;
private string applicationQueue;
private int machineID;
private int boardID;
private int channelID;
private string applicationName = "";
public ApplicationInfo()
{
}
public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)
{
session = (Guid)info.GetValue("session", typeof(Guid));
sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));
ANI = (string)info.GetValue("ANI", typeof(string));
DNIS = (string)info.GetValue("DNIS", typeof(string));
mqNW = (string)info.GetValue("mqNW", typeof(string));
parent = (string)info.GetValue("parent", typeof(string));
child = (string)info.GetValue("child", typeof(string));
applicationQueue = (string)info.GetValue("applicationQueue",typeof(string));
applicationName = (string)info.GetValue("applicationName", typeof(string));
machineID = (int)info.GetValue("machineID", typeof(int));
boardID = (int)info.GetValue("boardID", typeof(int));
channelID = (int)info.GetValue("channelID", typeof(int));
}

public ApplicationInfo(string session, string sessionNW, string ANI, string
DNIS, string mqNW, string parent, string child, string applicationQueue, int
machine, int board, int channel, string applicationName)
{
this.session = new Guid(session);
this.sessionNW = new Guid(sessionNW);
this.ANI = ANI;
this.DNIS = DNIS;
this.mqNW = mqNW;
this.parent = parent;
this.child = child;
this.applicationQueue = applicationQueue;
this.machineID = machine;
this.boardID = board;
this.channelID = channel;
this.applicationName = applicationName;
}

#region IDisposable Members
public void Dispose()
{
// TODO: Add ApplicationInfo.Dispose implementation
}
#endregion

#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("session", session);
info.AddValue("sessionNW", sessionNW);
info.AddValue("ANI", ANI);
info.AddValue("DNIS", DNIS);
info.AddValue("mqNW", mqNW);
info.AddValue("parent", parent);
info.AddValue("child", child);
info.AddValue("applicationQueue", applicationQueue);
info.AddValue("machineID", machineID);
info.AddValue("boardID", boardID);
info.AddValue("channelID", channelID);
info.AddValue("applicationName", applicationName);
}
#endregion
}

..........

ApplicationInfo app;
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, app);


MessageQueue mq = new MessageQueue(mqPath);
Message m = new Message(stream, new BinaryMessageFormatter());
mq.Formatter = new BinaryMessageFormatter();
mq.Send(m);

.........


public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)
{
try
{
MessageQueue mq = (MessageQueue)source;
Message m = mq.EndReceive(asyncResult.AsyncResult);
m.Formatter = new BinaryMessageFormatter();
incomingMessage(source, new
IncomingMessageEventArgs((object)m.BodyStream,false));
mq.BeginReceive();
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

......

public void OnIncomingMessage(object source, IncomingMessageEventArgs e)
{
BinaryFormatter formatter = new BinaryFormatter();
application = null;
application = (ApplicationInfo)formatter.Deserialize((Stream)e.getDocument);
}
 
Igor,

While I haven't taken the time to run the code, I can see that there is
nothing in your class that requires custom serialization. All you have to
do is add the Serializable attribute, and your serialization should work
fine. ISerializable only needs to be implemented when there are other
things (besides the fields in your class) that you want to serialize, or
they require certain logic to be executed.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Igor said:
[Serializable()]

public class ApplicationInfo:IDisposable,ISerializable

{

private Guid session; // 16 bytes

private Guid sessionNW; // 16 bytes

private string ANI;

private string DNIS;

private string mqNW; // networking queue

private string parent;

private string child;

private string applicationQueue;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo()

{

}

public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)

{

session = (Guid)info.GetValue("session", typeof(Guid));

sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));

ANI = (string)info.GetValue("ANI", typeof(string));

DNIS = (string)info.GetValue("DNIS", typeof(string));

mqNW = (string)info.GetValue("mqNW", typeof(string));

parent = (string)info.GetValue("parent", typeof(string));

child = (string)info.GetValue("child", typeof(string));

applicationQueue = (string)info.GetValue("applicationQueue",
typeof(string));

applicationName = (string)info.GetValue("applicationName",
typeof(string));

machineID = (int)info.GetValue("machineID", typeof(int));

boardID = (int)info.GetValue("boardID", typeof(int));

channelID = (int)info.GetValue("channelID", typeof(int));

}

public ApplicationInfo(string session, string sessionNW, string ANI,
string
DNIS, string mqNW, string parent, string child, string applicationQueue,
int
machine, int board, int channel, string applicationName)

{

this.session = new Guid(session);

this.sessionNW = new Guid(sessionNW);

this.ANI = ANI;

this.DNIS = DNIS;

this.mqNW = mqNW;

this.parent = parent;

this.child = child;

this.applicationQueue = applicationQueue;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicationName = applicationName;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo.Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext
context)

{

info.AddValue("session", session);

info.AddValue("sessionNW", sessionNW);

info.AddValue("ANI", ANI);

info.AddValue("DNIS", DNIS);

info.AddValue("mqNW", mqNW);

info.AddValue("parent", parent);

info.AddValue("child", child);

info.AddValue("applicationQueue", applicationQueue);

info.AddValue("machineID", machineID);

info.AddValue("boardID", boardID);

info.AddValue("channelID", channelID);

info.AddValue("applicationName", applicationName);

}

#endregion

}

.........

ApplicationInfo app;

MemoryStream stream = new MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, app);



MessageQueue mq = new MessageQueue(mqPath);

Message m = new Message(stream, new BinaryMessageFormatter());

mq.Formatter = new BinaryMessageFormatter();

mq.Send(m);



........



public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)source;

Message m = mq.EndReceive(asyncResult.AsyncResult);



m.Formatter = new BinaryMessageFormatter();

incomingMessage(source, new IncomingMessageEventArgs((object)m.BodyStream,
false));

mq.BeginReceive();

}

catch(Exception ex)

{

Console.WriteLine(ex.ToString());

}

}



.....



public void OnIncomingMessage(object source, IncomingMessageEventArgs e)

{

BinaryFormatter formatter = new BinaryFormatter();

application = null;

application =
(ApplicationInfo)formatter.Deserialize((Stream)e.getDocument);

}
 
Earlier I tried only with [Serializable()] attribute, it didnt worked
either. :(


Nicholas Paldino said:
Igor,

While I haven't taken the time to run the code, I can see that there is
nothing in your class that requires custom serialization. All you have to
do is add the Serializable attribute, and your serialization should work
fine. ISerializable only needs to be implemented when there are other
things (besides the fields in your class) that you want to serialize, or
they require certain logic to be executed.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Igor said:
[Serializable()]

public class ApplicationInfo:IDisposable,ISerializable

{

private Guid session; // 16 bytes

private Guid sessionNW; // 16 bytes

private string ANI;

private string DNIS;

private string mqNW; // networking queue

private string parent;

private string child;

private string applicationQueue;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo()

{

}

public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)

{

session = (Guid)info.GetValue("session", typeof(Guid));

sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));

ANI = (string)info.GetValue("ANI", typeof(string));

DNIS = (string)info.GetValue("DNIS", typeof(string));

mqNW = (string)info.GetValue("mqNW", typeof(string));

parent = (string)info.GetValue("parent", typeof(string));

child = (string)info.GetValue("child", typeof(string));

applicationQueue = (string)info.GetValue("applicationQueue",
typeof(string));

applicationName = (string)info.GetValue("applicationName",
typeof(string));

machineID = (int)info.GetValue("machineID", typeof(int));

boardID = (int)info.GetValue("boardID", typeof(int));

channelID = (int)info.GetValue("channelID", typeof(int));

}

public ApplicationInfo(string session, string sessionNW, string ANI,
string
DNIS, string mqNW, string parent, string child, string applicationQueue,
int
machine, int board, int channel, string applicationName)

{

this.session = new Guid(session);

this.sessionNW = new Guid(sessionNW);

this.ANI = ANI;

this.DNIS = DNIS;

this.mqNW = mqNW;

this.parent = parent;

this.child = child;

this.applicationQueue = applicationQueue;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicationName = applicationName;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo.Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext
context)

{

info.AddValue("session", session);

info.AddValue("sessionNW", sessionNW);

info.AddValue("ANI", ANI);

info.AddValue("DNIS", DNIS);

info.AddValue("mqNW", mqNW);

info.AddValue("parent", parent);

info.AddValue("child", child);

info.AddValue("applicationQueue", applicationQueue);

info.AddValue("machineID", machineID);

info.AddValue("boardID", boardID);

info.AddValue("channelID", channelID);

info.AddValue("applicationName", applicationName);

}

#endregion

}

.........

ApplicationInfo app;

MemoryStream stream = new MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, app);



MessageQueue mq = new MessageQueue(mqPath);

Message m = new Message(stream, new BinaryMessageFormatter());

mq.Formatter = new BinaryMessageFormatter();

mq.Send(m);



........



public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)source;

Message m = mq.EndReceive(asyncResult.AsyncResult);



m.Formatter = new BinaryMessageFormatter();

incomingMessage(source, new IncomingMessageEventArgs((object)m.BodyStream,
false));

mq.BeginReceive();

}

catch(Exception ex)

{

Console.WriteLine(ex.ToString());

}

}



.....



public void OnIncomingMessage(object source, IncomingMessageEventArgs e)

{

BinaryFormatter formatter = new BinaryFormatter();

application = null;

application =
(ApplicationInfo)formatter.Deserialize((Stream)e.getDocument);

}









Jon Skeet said:
While executing BinaryFormatter.Deserialize() I get:
System.InvalidCastException: Specified cast is not valid.
I implemented ISerializable interface. What may be a problem?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Igor,

What is the problem when you have JUST the serializable attribute, and
remove the serialization constructor and the implementation of
ISerializable?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Igor said:
Earlier I tried only with [Serializable()] attribute, it didnt worked
either. :(


in
message news:[email protected]...
Igor,

While I haven't taken the time to run the code, I can see that there is
nothing in your class that requires custom serialization. All you have
to
do is add the Serializable attribute, and your serialization should work
fine. ISerializable only needs to be implemented when there are other
things (besides the fields in your class) that you want to serialize, or
they require certain logic to be executed.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Igor said:
[Serializable()]

public class ApplicationInfo:IDisposable,ISerializable

{

private Guid session; // 16 bytes

private Guid sessionNW; // 16 bytes

private string ANI;

private string DNIS;

private string mqNW; // networking queue

private string parent;

private string child;

private string applicationQueue;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo()

{

}

public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)

{

session = (Guid)info.GetValue("session", typeof(Guid));

sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));

ANI = (string)info.GetValue("ANI", typeof(string));

DNIS = (string)info.GetValue("DNIS", typeof(string));

mqNW = (string)info.GetValue("mqNW", typeof(string));

parent = (string)info.GetValue("parent", typeof(string));

child = (string)info.GetValue("child", typeof(string));

applicationQueue = (string)info.GetValue("applicationQueue",
typeof(string));

applicationName = (string)info.GetValue("applicationName",
typeof(string));

machineID = (int)info.GetValue("machineID", typeof(int));

boardID = (int)info.GetValue("boardID", typeof(int));

channelID = (int)info.GetValue("channelID", typeof(int));

}

public ApplicationInfo(string session, string sessionNW, string ANI,
string
DNIS, string mqNW, string parent, string child, string
applicationQueue,
int
machine, int board, int channel, string applicationName)

{

this.session = new Guid(session);

this.sessionNW = new Guid(sessionNW);

this.ANI = ANI;

this.DNIS = DNIS;

this.mqNW = mqNW;

this.parent = parent;

this.child = child;

this.applicationQueue = applicationQueue;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicationName = applicationName;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo.Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext
context)

{

info.AddValue("session", session);

info.AddValue("sessionNW", sessionNW);

info.AddValue("ANI", ANI);

info.AddValue("DNIS", DNIS);

info.AddValue("mqNW", mqNW);

info.AddValue("parent", parent);

info.AddValue("child", child);

info.AddValue("applicationQueue", applicationQueue);

info.AddValue("machineID", machineID);

info.AddValue("boardID", boardID);

info.AddValue("channelID", channelID);

info.AddValue("applicationName", applicationName);

}

#endregion

}

.........

ApplicationInfo app;

MemoryStream stream = new MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, app);



MessageQueue mq = new MessageQueue(mqPath);

Message m = new Message(stream, new BinaryMessageFormatter());

mq.Formatter = new BinaryMessageFormatter();

mq.Send(m);



........



public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)source;

Message m = mq.EndReceive(asyncResult.AsyncResult);



m.Formatter = new BinaryMessageFormatter();

incomingMessage(source, new IncomingMessageEventArgs((object)m.BodyStream,
false));

mq.BeginReceive();

}

catch(Exception ex)

{

Console.WriteLine(ex.ToString());

}

}



.....



public void OnIncomingMessage(object source, IncomingMessageEventArgs
e)

{

BinaryFormatter formatter = new BinaryFormatter();

application = null;

application =
(ApplicationInfo)formatter.Deserialize((Stream)e.getDocument);

}









While executing BinaryFormatter.Deserialize() I get:
System.InvalidCastException: Specified cast is not valid.
I implemented ISerializable interface. What may be a problem?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Thats what I did and I got the same exception


Nicholas Paldino said:
Igor,

What is the problem when you have JUST the serializable attribute, and
remove the serialization constructor and the implementation of
ISerializable?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Igor said:
Earlier I tried only with [Serializable()] attribute, it didnt worked
either. :(


in
message news:[email protected]...
Igor,

While I haven't taken the time to run the code, I can see that
there
is
nothing in your class that requires custom serialization. All you have
to
do is add the Serializable attribute, and your serialization should work
fine. ISerializable only needs to be implemented when there are other
things (besides the fields in your class) that you want to serialize, or
they require certain logic to be executed.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

[Serializable()]

public class ApplicationInfo:IDisposable,ISerializable

{

private Guid session; // 16 bytes

private Guid sessionNW; // 16 bytes

private string ANI;

private string DNIS;

private string mqNW; // networking queue

private string parent;

private string child;

private string applicationQueue;

private int machineID;

private int boardID;

private int channelID;

private string applicationName = "";

public ApplicationInfo()

{

}

public ApplicationInfo(SerializationInfo info, StreamingContext ctxt)

{

session = (Guid)info.GetValue("session", typeof(Guid));

sessionNW = (Guid)info.GetValue("sessionNW", typeof(Guid));

ANI = (string)info.GetValue("ANI", typeof(string));

DNIS = (string)info.GetValue("DNIS", typeof(string));

mqNW = (string)info.GetValue("mqNW", typeof(string));

parent = (string)info.GetValue("parent", typeof(string));

child = (string)info.GetValue("child", typeof(string));

applicationQueue = (string)info.GetValue("applicationQueue",
typeof(string));

applicationName = (string)info.GetValue("applicationName",
typeof(string));

machineID = (int)info.GetValue("machineID", typeof(int));

boardID = (int)info.GetValue("boardID", typeof(int));

channelID = (int)info.GetValue("channelID", typeof(int));

}

public ApplicationInfo(string session, string sessionNW, string ANI,
string
DNIS, string mqNW, string parent, string child, string
applicationQueue,
int
machine, int board, int channel, string applicationName)

{

this.session = new Guid(session);

this.sessionNW = new Guid(sessionNW);

this.ANI = ANI;

this.DNIS = DNIS;

this.mqNW = mqNW;

this.parent = parent;

this.child = child;

this.applicationQueue = applicationQueue;

this.machineID = machine;

this.boardID = board;

this.channelID = channel;

this.applicationName = applicationName;

}

#region IDisposable Members

public void Dispose()

{

// TODO: Add ApplicationInfo.Dispose implementation

}

#endregion

#region ISerializable Members

public void GetObjectData(SerializationInfo info, StreamingContext
context)

{

info.AddValue("session", session);

info.AddValue("sessionNW", sessionNW);

info.AddValue("ANI", ANI);

info.AddValue("DNIS", DNIS);

info.AddValue("mqNW", mqNW);

info.AddValue("parent", parent);

info.AddValue("child", child);

info.AddValue("applicationQueue", applicationQueue);

info.AddValue("machineID", machineID);

info.AddValue("boardID", boardID);

info.AddValue("channelID", channelID);

info.AddValue("applicationName", applicationName);

}

#endregion

}

.........

ApplicationInfo app;

MemoryStream stream = new MemoryStream();

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream, app);



MessageQueue mq = new MessageQueue(mqPath);

Message m = new Message(stream, new BinaryMessageFormatter());

mq.Formatter = new BinaryMessageFormatter();

mq.Send(m);



........



public void OnReceiveCompleted(Object source, ReceiveCompletedEventArgs
asyncResult)

{

try

{

MessageQueue mq = (MessageQueue)source;

Message m = mq.EndReceive(asyncResult.AsyncResult);



m.Formatter = new BinaryMessageFormatter();

incomingMessage(source, new IncomingMessageEventArgs((object)m.BodyStream,
false));

mq.BeginReceive();

}

catch(Exception ex)

{

Console.WriteLine(ex.ToString());

}

}



.....



public void OnIncomingMessage(object source, IncomingMessageEventArgs
e)

{

BinaryFormatter formatter = new BinaryFormatter();

application = null;

application =
(ApplicationInfo)formatter.Deserialize((Stream)e.getDocument);

}









While executing BinaryFormatter.Deserialize() I get:
System.InvalidCastException: Specified cast is not valid.
I implemented ISerializable interface. What may be a problem?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
application = (ApplicationInfo)formatter.Deserialize((Stream)e.getDocument);

What tells splitting the above compound statement into a sequence of simple
statements you? What part gives the error? What are the actual types
involved? Make sure you indeed get back what you expect, show us the
splitted version and the real types involved. Sofar I see nothing wrong but
you have an error that pops up in the above line so there must be some
mismatch...
 
object obj = formatter.Deserialize((MemoryStream)e.getDocument);

ApplicationInfo .ApplicationInfo application = (ApplicationInfo.ApplicationInfo)obj;

I get that exception in second row.



In both applications, sender and receiver class defined:
namespace ApplicationInfo

{

[Serializable()]

public class ApplicationInfo:IDisposable

{

...

}
}
 
Back
Top