c#将hashtable序列化保存到cookie的问题!

yangydp 2010-03-15 03:34:46
我想实现将购物车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,则读取正常.
...全文
321 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gzdotcom 2010-03-15
  • 打赏
  • 举报
回复
是Server.UrlDecode的问题,你换为用base64试下,应该就可以了.
阿非 2010-03-15
  • 打赏
  • 举报
回复
你断点跟一下, 确定一下 是否序列化前 是正确的,反序列化后是错误的
yangydp 2010-03-15
  • 打赏
  • 举报
回复
希望高手帮忙解决一下,分不够可以加.
yangydp 2010-03-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yangydp 的回复:]
例如:如果是2300,结果就变为:2111;如果是2500,就变为2367
[/Quote]

这个结果是我把double类型改为int类型后的结果,我以为是double类型的问题,改为int后也是不对.
yangydp 2010-03-15
  • 打赏
  • 举报
回复
例如:如果是2300,结果就变为:2111;如果是2500,就变为2367
雨网科技 2010-03-15
  • 打赏
  • 举报
回复
哦 这还真不是一般问题,后面的0这么会不认识呢 前面的0不认识还差不多
阿非 2010-03-15
  • 打赏
  • 举报
回复
举一个偏差的例子

110,477

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧