调用WebService时,传入Xml文本,反序列化成对象的时候报错问题?
报:无法将类型为localhost.Product”的对象强制转换为类型“Model.Product”错误
调用如下:
//访问WebService
localhost.ThirdParty myservice = new localhost.ThirdParty();
Product product = (Product)myservice.Deserialization();
Response.Write(product.Name + "<br>" + product.Price + "<br>" + product.ZhiZao + "<br>" + product.Number);
Product类型定义如下
namespace Model
{
[Serializable]
public class Product:GetInsertUpdateObjectBase
{
private string _name { get; set; }
private double _price { get; set; }
private string _zhizao { get; set; }
/// <summary>
/// 名称
/// </summary>
public string Name
{
get { return this._name; }
set { this._name = value; }
}
/// <summary>
/// 价格
/// </summary>
public double Price
{
get { return this._price; }
set { this._price = value; }
}
/// <summary>
/// 制造
/// </summary>
public string ZhiZao
{
get { return this._zhizao; }
set { this._zhizao = value; }
}
}
}
GetInsertUpdateObjectBase定义如下
namespace Interface
{
public interface GetInsertUpdateObjectBase
{
}
}
XmlManager定义如下
/// <summary>
/// Xml序列化和反序列化
/// </summary>
public class XmlManager
{
/// <summary>
/// Xml序列化
/// </summary>
/// <param name="type">类型</param>
/// <param name="obj">对象</param>
/// <returns></returns>
public static string Serializer(Type type, object obj)
{
MemoryStream stream = new MemoryStream();
//创建序列化对象
XmlSerializer xs = new XmlSerializer(type);
try
{
xs.Serialize(stream, obj);
}
catch(Exception ex)
{
}
stream.Position = 0;
//创建流读取器
StreamReader sr = new StreamReader(stream);
string strContent = sr.ReadToEnd();//从流中读取内容
return strContent;
}
/// <summary>
/// Xml反序列化
/// </summary>
/// <param name="type">类类型</param>
/// <param name="strXmlInfo">字符串</param>
/// <returns>对象</returns>
public static object Deserializer(Type type, string strXmlInfo)
{
using (StringReader sr = new StringReader(strXmlInfo))
{
XmlSerializer xs = new XmlSerializer(type);
return xs.Deserialize(sr);
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <param name="type">类类型</param>
/// <param name="stream">流</param>
/// <returns>对象</returns>
public static object Deserializer(Type type, Stream stream)
{
XmlSerializer xs = new XmlSerializer(type);
return xs.Deserialize(stream);
}
}
//反序列化
public object Deserialization()
{
string strXmlInfo = "<Product><Name>201122</Name><Price>4500</Price><ZhiZao>美国</ZhiZao><Number>10</Number></Product>";
GetInsertUpdateObjectBase product;
string strFilePath = @"C:\网站\公司\网站\某某网\Model\bin\Debug\Model.dll";//程序集文件路径
string strNameSpace = "Model.Product";//实例对象的命名空间
product = (GetInsertUpdateObjectBase)ReflectionInsert(strFilePath, strNameSpace, strXmlInfo);
return product;
}
//利用反射反序列化
public static object ReflectionInsert(string strFilePath, string strNameSpace, string strXmlInfo)
{
GetInsertUpdateObjectBase iobject = null;//接口类型
try
{
//加载指定路径上的程序集文件的内容
Assembly ass = (Assembly)Assembly.LoadFile(strFilePath);
//获取程序集实例中具有指定名称的 Type 对象
iobject = (GetInsertUpdateObjectBase)XmlManager.Deserializer(ass.GetType(strNameSpace), strXmlInfo);
}
catch (Exception)
{
}
return iobject;
}
[解决办法]
序列化的方法不能使用 object 类型 ,换一个类型试试吧!
[解决办法]