WebService 序列化反序列化 反悔自定义对象错误,各路大侠来看看。
[Serializable][XmlInclude(typeof(Model.yhModel))]//Model.yhModel这个是数据层的publicclassWeb...
[Serializable]
[XmlInclude(typeof(Model.yhModel))]//Model.yhModel这个是数据层的
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public byte[] SerializeObject(object pObj)//序列化
{
if (pObj == null)
{
return null;
}
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, pObj);//内存流 pObj转化的
memoryStream.Position = 0;
byte[] read = new byte[memoryStream.Length];
memoryStream.Read(read, 0, read.Length);
memoryStream.Close();
return read;
}
[WebMethod]
public object DeserializeObject(byte[] pBytes)//反序列化
{
object newOjb = null;
if (pBytes == null)
{
return newOjb;
}
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(pBytes);
memoryStream.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
newOjb = formatter.Deserialize(memoryStream);
memoryStream.Close();
return newOjb;
}
[WebMethod]
public byte[] yhModel_sele(string bh)//返回yhModel对象的序列。
{
Model.yhModel model = new Model.yhModel();
Dal.yh.yhDal dal = new Dal.yh.yhDal();
model = dal.Select(bh);
byte[] r;
r = SerializeObject(model);
return r;
}
}
以上是WebService1的定义.
private void Login()
{
...多余代码略
ServiceReference1.WebService1SoapClient a = new ServiceReference1.WebService1SoapClient("WebService1Soap");
Model.yhModel _yhModel = new Model.yhModel();
byte[] r = a.yhModel_sele(this.textBoxX1.Text.ToString());
object obj = a.DeserializeObject(r);//这一步确实能到的序列,在web里直接打开也是能获得序列的
_yhModel = (Model.yhModel)obj;//<---报错在这里。
}
报错提示:无法将类型为“Test.ServiceReference1.yhModel”的对象强制转换为类型“Model.yhModel”。
我就不明白了,在WebService引用的Model.yhModel怎么变成了“Test.ServiceReference1.yhModel”搞得无法将对象转换回原有的类型呢?? 展开
[XmlInclude(typeof(Model.yhModel))]//Model.yhModel这个是数据层的
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public byte[] SerializeObject(object pObj)//序列化
{
if (pObj == null)
{
return null;
}
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, pObj);//内存流 pObj转化的
memoryStream.Position = 0;
byte[] read = new byte[memoryStream.Length];
memoryStream.Read(read, 0, read.Length);
memoryStream.Close();
return read;
}
[WebMethod]
public object DeserializeObject(byte[] pBytes)//反序列化
{
object newOjb = null;
if (pBytes == null)
{
return newOjb;
}
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(pBytes);
memoryStream.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
newOjb = formatter.Deserialize(memoryStream);
memoryStream.Close();
return newOjb;
}
[WebMethod]
public byte[] yhModel_sele(string bh)//返回yhModel对象的序列。
{
Model.yhModel model = new Model.yhModel();
Dal.yh.yhDal dal = new Dal.yh.yhDal();
model = dal.Select(bh);
byte[] r;
r = SerializeObject(model);
return r;
}
}
以上是WebService1的定义.
private void Login()
{
...多余代码略
ServiceReference1.WebService1SoapClient a = new ServiceReference1.WebService1SoapClient("WebService1Soap");
Model.yhModel _yhModel = new Model.yhModel();
byte[] r = a.yhModel_sele(this.textBoxX1.Text.ToString());
object obj = a.DeserializeObject(r);//这一步确实能到的序列,在web里直接打开也是能获得序列的
_yhModel = (Model.yhModel)obj;//<---报错在这里。
}
报错提示:无法将类型为“Test.ServiceReference1.yhModel”的对象强制转换为类型“Model.yhModel”。
我就不明白了,在WebService引用的Model.yhModel怎么变成了“Test.ServiceReference1.yhModel”搞得无法将对象转换回原有的类型呢?? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询