BinaryFormatter.Deserialize [英] BinaryFormatter.Deserialize
问题描述
嗨。
执行BinaryFormatter.Deserialize()时,我得到:
System.InvalidCastException:指定的强制转换无效。
我实现了ISerializable接口。可能有什么问题?
谢谢。
Igor< no *** @ hotmail .COM>写道:执行BinaryFormatter.Deserialize()时,我得到:
System.InvalidCastException:指定的强制转换无效。
我实现了ISerializable接口。可能有什么问题?
你能发一个简短但完整的程序来演示
问题吗?
>
请参阅 http://www.pobox.com /~skeet/csharp/complete.html 了解详情
我的意思是什么。
-
Jon Skeet - < sk *** @ pobox.com>
http ://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
[Serializable()]
公共类ApplicationInfo:IDisposable,ISerializable
{
private指导会议; // 16字节
私人Guid sessionNW; // 16字节
私有字符串ANI;
私有字符串DNIS;
私有字符串mqNW; //网络队列
私有字符串父级;
私有字符串子级;
私有字符串applicationQueue;
private int machineID;
private int boardID;
private int channelID;
私有字符串applicationName ="" ;;
public ApplicationInfo()
{
}
公共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 =新Guid(会话);
this.sessionNW =新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;
这个。 channelID = channel;
this.applicationName = applicationName;
}
#region IDisposable会员
public void Dispose()
{
// TODO:添加ApplicationInfo。配置实施
}
#endregion
#region ISerializable会员
public void GetObjectData(SerializationInfo info,StreamingContext context)
{
info.AddValue(" session" ,会话);
info.AddValue(" sessionNW",sessionNW);
info.AddValue(" ANI&quo t;,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 < br $>
}
..........
ApplicationInfo app;
MemoryStream流=新的MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream,app);
MessageQueue mq = new MessageQueue(mqPath);
M essage m = new Message(stream,new BinaryMessageFormatter());
mq.Formatter = new BinaryMessageFormatter();
mq.Send (m);
.........
public void OnReceiveCompleted(Object source,ReceiveCompletedEventArgs
asyncResult)
{
试试
{
MessageQueue mq =(MessageQueue)source;
消息m = mq.EndReceive(asyncResult.AsyncResult);
m.Formatter = new BinaryMessageFormatter();
incomingMessage(source,new IncomingMessageEventArgs((object)m.BodyStream,
false));
mq.BeginReceive();
}
catch(Exception ex)
< br $>
{
Console.WriteLine(ex.ToString());
}
}
......
public void OnIncomingMessage(对象源,IncomingMessageEventArgs e)
$
BinaryFormatter formatter = new BinaryFormatter();
application = null;
application =(ApplicationInfo)formatter.Deserialize((Stream)eg etDocument);
}
Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道
新闻:MP ************************ @ msnews.microsoft.c om ...Igor< no *** @ hotmail.com>写道:执行BinaryFormatter.Deserialize()时,我得到:
System.InvalidCastException:指定的强制转换无效。
我实现了ISerializable接口。可能有什么问题?
你能发一个简短但完整的程序来演示
问题吗?
参见 http://www.pobox.com/~skeet/csharp/complete.html 详细信息
我的意思是什么。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复群组,请不要给我发邮件
Igor< no *** @ hotmail.com>写道:
< snip>
参见 http://www.pobox.com/~skeet/csharp/incomplete.html
如果您没有以这样的方式发布代码,那么这将有助于每个实际代码行之间还有一个空白行。
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
Hi.
While executing BinaryFormatter.Deserialize() I get:
System.InvalidCastException: Specified cast is not valid.
I implemented ISerializable interface. What may be a problem?
Thanks.
Igor <no***@hotmail.com> wrote: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.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
[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.g etDocument);
}
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...Igor <no***@hotmail.com> wrote: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.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Igor <no***@hotmail.com> wrote:
<snip>
See http://www.pobox.com/~skeet/csharp/incomplete.html
It would help if you didn''t post the code in such a way that there''s a
blank line between each real line of code, too.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于BinaryFormatter.Deserialize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!