c#将hashtable序列化保存到cookie的问题!
我想实现将购物车hashtable序列化后保存到cookie,但是从cookie里将数据反序列化时,原先保存在hashtable里的Price变量的值不对了(所有double类型的变量都会变,如果值小于100好像就正常),不知什么原因?
shopCart.cs类
[Serializable]
public class ShopCart
{
public Hashtable _CartItems = new Hashtable();
public ShopCart()
{
///to do something
}
public ICollection CartItems
{
get { return _CartItems.Values; }
}
public double Total
{
get
{
double sum = 0;
foreach (ShopCartItem item in _CartItems.Values)
{
sum += ((item.Price * item.Quantity) + item.SendPrice);
}
return sum;
}
}
public double TotalNum
{
get
{
double sum = 0;
foreach (ShopCartItem item in _CartItems.Values)
{
sum += item.Quantity;
}
return sum;
}
}
public void AddItem(string ID, string Name, string DeliveryName, double Price, int Score, string ProductId, string PicUrl, double MarketPrice, double UserPrice, double VipPrice, double SendPrice, string ShopID, string UrlFrom, bool AutoAddQuantity)
{
ShopCartItem item = (ShopCartItem)_CartItems[ID];
if (item == null)
_CartItems.Add(ID, new ShopCartItem(ID, Name, DeliveryName, Price, Score, ProductId, PicUrl, MarketPrice, UserPrice, VipPrice, SendPrice, ShopID, UrlFrom));
else
{
if (AutoAddQuantity)
{
item.Quantity++;
}
_CartItems[ID] = item;
}
}
}
/// <summary>
/// [购物车具体]商品类
/// </summary>
[Serializable]
public class ShopCartItem
{
private string _ID;//产品GUID
private string _Name;//产品名称
private string _DeliveryName;//物流产品名
private double _Price = 0;//产品单价(结算价格)
private int _Score = 0;//产品单个积分
private int _Quantity = 1;//产品数量
private string _ProductId;//产品自定义编号
private string _PicUrl;//图片地址
private double _MarketPrice = 0;//市场价格
private double _VipPrice = 0;//VIp价格
private double _UserPrice = 0;//会员价格
private double _SendPrice = 0;//运输费用
private string _ShopID;//商家ID
private string _UrlFrom; //页面来源
/// <summary>
/// 属性:商品ID
/// </summary>
public string ID
{
get { return _ID; }
}
/// <summary>
/// 属性:商品名称Name
/// </summary>
public string Name
{
get { return _Name; }
}
/// <summary>
/// 属性:商品单价Price
/// </summary>
public double Price
{
get { return _Price; }
}
/// <summary>
/// 单件产品所获积分
/// </summary>
public int Score
{
get
{
return _Score;
}
set
{
_Score = value;
}
}
/// <summary>
/// 产品编号
/// </summary>
public string ProductId
{
get
{
return _ProductId;
}
set
{
_ProductId = value;
}
}
/// <summary>
/// 产品图片地址
/// </summary>
public string PicUrl
{
get
{
return _PicUrl;
}
set
{
_PicUrl = value;
}
}
/// <summary>
/// 市场价格
/// </summary>
public double MarketPrice
{
get
{
return _MarketPrice;
}
set
{
_MarketPrice = value;
}
}
/// <summary>
/// VIP价格
/// </summary>
public double VipPrice
{
get
{
return _VipPrice;
}
set
{
_VipPrice = value;
}
}
/// <summary>
/// 会员价格
/// </summary>
public double UserPrice
{
get
{
return _UserPrice;
}
set
{
_UserPrice = value;
}
}
/// <summary>
/// 运输费用
/// </summary>
public double SendPrice
{
get
{
return _SendPrice;
}
set
{
_SendPrice = value;
}
}
/// <summary>
/// 属性:商品数量Quantity
/// </summary>
public int Quantity
{
get { return _Quantity; }
set { _Quantity = value; }
}
/// <summary>
/// 商家ID
/// </summary>
public string ShopID
{
get { return _ShopID; }
set { _ShopID = value; }
}
/// <summary>
/// 页面来源
/// </summary>
public string UrlFrom
{
get { return _UrlFrom; }
set { _UrlFrom = value; }
}
public ShopCartItem(string ID, string Name, string DeliveryName, double Price, int Score, string ProductId, string PicUrl, double MarketPrice, double UserPrice, double VipPrice, double SendPrice, string ShopID, string UrlFrom)
{
_ID = ID;
_Name = Name;
_DeliveryName = DeliveryName;
_Quantity = 1;
_Price = Price;
_Score = Score;
_ProductId = ProductId;
_PicUrl = PicUrl;
_MarketPrice = MarketPrice;
_UserPrice = UserPrice;
_VipPrice = VipPrice;
_SendPrice = SendPrice;
_ShopID = ShopID;
_UrlFrom = UrlFrom;
}
}
调用例子:
ShopCart SC = new ShopCart();
SC.AddItem("1", "ProductName", "ProductName", 2300,8000, "ProductID", "", 7000, 6000, 5000, 4000, "ShopId", "TestUrl", true);
//将ShopCart对象写入Cookie
IFormatter fm = new BinaryFormatter();
Stream sm = new MemoryStream();
fm.Serialize(sm, SC);
sm.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(sm);
string strCart = reader.ReadToEnd();
reader.Close();
HttpCookie hc = new HttpCookie(CookieName);
hc.Value = Server.UrlEncode(strCart);
hc.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(hc);
Response.Write("<br>");
string StrCartNew = Server.UrlDecode(Request.Cookies[CookieName].Value.ToString());
byte[] bt = System.Text.Encoding.Default.GetBytes(StrCartNew);
Stream smNew = new MemoryStream(bt);
IFormatter fmNew = new BinaryFormatter();
ShopCart SCNew = (ShopCart)fmNew.Deserialize(smNew);
lblResult.Text = "";
foreach (ShopCartItem SCI in SCNew.CartItems)
{
lblResult.Text += "<br/>产品价格:"+ SCI.Price;
}
最后读出来的价格不是"2300",如果价格是23,则读取正常.