无法将类型为“System.Double”的对象强制转换为“_Default”。 [英] Unable to cast object of type 'System.Double' to type '_Default'.

查看:144
本文介绍了无法将类型为“System.Double”的对象强制转换为“_Default”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发生错误无法将System.Double类型的对象强制转换为_Default。



Error occured Unable to cast object of type 'System.Double' to type '_Default'.

[Serializable]
  public partial class _Default : System.Web.UI.Page
  {
      BinaryFormatter obj1 = new BinaryFormatter();

          public Int64 AccNo;
          public double TranAmt;
          public string custName;
          FileStream fsrw;


      protected void Page_Load(object sender, EventArgs e)
      {

      }
      protected void btnSerilize_Click(object sender, EventArgs e)
      {
          try
          {
              AccNo = Convert.ToInt64(TextBox1.Text);
              custName = TextBox2.Text;
              TranAmt = Convert.ToDouble(TextBox3.Text);
              fsrw = new FileStream("C:\\Users\\flash\\Desktop\\Serilization.txt", FileMode.Create, FileAccess.Write);
              obj1.Serialize(fsrw, TranAmt);
              obj1.Serialize(fsrw, AccNo);
              obj1.Serialize(fsrw, custName);
              fsrw.Flush();
              fsrw.Close();

              Response.Write("<script type='text/javascript'>");
              Response.Write("alert('Serilization Succeded');");
              Response.Write("</script>");
              TextBox1.Text = string.Empty;
              TextBox2.Text = string.Empty;
              TextBox3.Text = string.Empty;
          }
          catch (Exception ex)
          {
              Response.Write("<script type='text/javascript'>");
              Response.Write("alert('" + ex.Message + "')");
              Response.Write("</script>");
          }
      }
      protected void btnDeserilize_Click(object sender, EventArgs e)
      {
          try
          {
              _Default obj2 = new _Default();
              fsrw = new FileStream("C:\\Users\\flash\\Desktop\\Serilization.txt", FileMode.Open, FileAccess.Read);
              obj2=(_Default)obj1.Deserialize(fsrw);
              fsrw.Flush();
              fsrw.Close();
              TextBox1.Text = obj2.AccNo.ToString();
              TextBox2.Text = obj2.custName.ToString();
              TextBox3.Text = obj2.TranAmt.ToString();
              Response.Write("<script type='text/javascript'>");
              Response.Write("alert('Deserilization Succeded')");
              Response.Write("</script>");
          }
          catch (Exception ex)
          {
              Response.Write("<script type='text/javascript'>");
              Response.Write("alert('" + ex.Message + "')");
              Response.Write("</script>");
          }
      }
}









当我试图反序列化并分配给对象时引发错误

这里的代码是我得到错误



obj2 = (_Default)obj1.Deserialize(fsrw);





Error raised when im trying to deserialize and assigning to the object
here is the Code where im getting error

obj2=(_Default)obj1.Deserialize(fsrw);

推荐答案

创建一个新类,如果你愿意,甚至是一个子类,它有三个你想要跟踪的项目。



另外,使用是你的朋友;内存泄漏不是。

Make a new class, even a child class if you like, that has the three items that you intend to track.

Also, using is your friend; memory leaks are not.
[Serializable]
public class MyTrackedItems
{
   public Int64 AccNo;
   public double TranAmt;
   public string custName;
}





然后更改你的页面:



Then change your page:

public partial class _Default : System.Web.UI.Page
{
   private BinaryFormatter obj1 = new BinaryFormatter(); 
   public MyTrackedItems itemInstance = new MyTrackedItems();

   protected void Page_Load(object sender, EventArgs e){}

   protected void btnSerilize_Click(object sender, EventArgs e)
   {
      try
      {
          itemInstance.AccNo= Convert.ToInt64(TextBox1.Text);
          itemInstance.custName = TextBox2.Text;
          itemInstance.TranAmt = Convert.ToDouble(TextBox3.Text);
          using(var fsrw = new FileStream("C:\\Users\\flash\\Desktop\\Serilization.txt", FileMode.Create, FileAccess.Write))
          {
             obj1.Serialize(fsrw, itemInstance);
             fsrw.Flush();
             fsrw.Close();
          }
          Response.Write("<script type='text/javascript'>");
          Response.Write("alert('Serilization Succeded');");
          Response.Write("</script>");
          TextBox1.Text = string.Empty;
          TextBox2.Text = string.Empty;
          TextBox3.Text = string.Empty;
       }
       catch (Exception ex)
       {
           Response.Write("<script type='text/javascript'>");
           Response.Write("alert('" + ex.Message + "')");
           Response.Write("</script>");
       }
   }
   protected void btnDeserilize_Click(object sender, EventArgs e)
   {
      try
      {
         MyTrackedItems obj2 = new MyTrackedItems();
         using(var fsrw = new FileStream("C:\\Users\\flash\\Desktop\\Serilization.txt", FileMode.Open, FileAccess.Read))
         {
            obj2=(MyTrackedItems)obj1.Deserialize(fsrw);
            fsrw.Flush();
            fsrw.Close();
          }
         TextBox1.Text = obj2.AccNo.ToString();
         TextBox2.Text = obj2.custName.ToString();
         TextBox3.Text = obj2.TranAmt.ToString();
         Response.Write("<script type='text/javascript'>");
         Response.Write("alert('Deserilization Succeded')");
         Response.Write("</script>");
       }
       catch (Exception ex)
       {
          Response.Write("<script type='text/javascript'>");
          Response.Write("alert('" + ex.Message + "')");
          Response.Write("</script>");
       }
   }
}


这篇关于无法将类型为“System.Double”的对象强制转换为“_Default”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆