开发者

Unhandled cast exception error

开发者 https://www.devze.com 2023-03-16 02:05 出处:网络
I\'m getting the following exception: Unable to cast object of type \'System.Collections.Generic.List`1[test.Form1+comodity]\' to type \'System.Collections.Generic.List`1[test.Form2+comodity]\'.

I'm getting the following exception:

Unable to cast object of type 'System.Collections.Generic.List`1[test.Form1+comodity]' to type 'System.Collections.Generic.List`1[test.Form2+comodity]'.

for this line:

comodityData = (List<comodity>)tf.Deserialize(ts);

and I'm not sure why. I have never ran into the error before. Can anyone help me out?

Full code for reference:

namespace test
{
   public partial class Form2 : Form
   {
      public float Alligor;
      public float Briochit;
      public float Chollonin;
      public float Espitium;
      public float Hydrobenol;
      public float Isopropenetol;
      public float Metachropin;
      public float Phlobotil;
      public float Plasteosine;
      public float Polynitrocol;
      public float Polynucleit;
      public float Prilumium;
      public float Statchanol;
      public float Titanium;
      public float Vitricyl;

      public float AlligorInput;
      public float BriochitInput;
      public float CholloninInput;
      public float EspitiumInput;
      public float HydrobenolInput;
      public float IsopropenetolInput;
      public float MetachropinInput;
      public float PhlobotilInput;
      public float PlasteosineInput;
      public float PolynitrocolInput;
      public float PolynucleitInput;
      public float PrilumiumInput;
      public float StatchanolInput;
      public float TitaniumInput;
      public float VitricylInput;

      public float costofAlligor;
      public float costofBriochit;
      public float costofChollonin;
      public float costofEspitium;
      public float costofHydrobenol;
      public float costofIsopropenetol;
      public float costofMetachropin;
      public float costofPhlobotil;
      public float costofPlasteosine;
      public float costofPolynitrocol;
      public float costofPolynucleit;
      public float costofPrilumium;
      public float costofStatchanol;
      public float costofTitanium;
      public float costofVitricyl;

      public double totalCost;

      public class comodity
      {
         public float comodityPrice;
      }

      private List<comodity> comodityData = new List<comodity>();
      private comodity o1 = null;
      private comodity o2 = null;
      private comodity o3 = null;
      private comodity o4 = null;
      private comodity o5 = null;
      private comodity o6 = null;
      private comodity o7 = null;
      private comodity o8 = null;
      private comodity o9 = null;
      private comodity o10 = null;
      private comodity o11 = null;
      private comodity o12 = null;
      private comodity o13 = null;
      private comodity o14 = null;
      private comodity o15 = null;

      public Form2()
      {
         InitializeComponent();
         o1 = new comodity();
         o2 = new comodity();
         o3 = new comodity();
         o4 = new comodity();
         o5 = new comodity();
         o6 = new comodity();
         o7 = new comodity();
         o8 = new comodity();
         o9 = new comodity();
         o10 = new comodity();
         o11 = new comodity();
         o12 = new comodity();
         o13 = new comodity();
         o14 = new comodity();
         o15 = new comodity();

         comodityData.Add(o1);
         comodityData.Add(o2);
         comodityData.Add(o3);
         comodityData.Add(o4);
         comodityData.Add(o5);
         comodityData.Add(o6);
         comodityData.Add(o7);
         comodityData.Add(o8);
         comodityData.Add(o9);
         comodityData.Add(o11);
         comodityData.Add(o12);
         comodityData.Add(o13);
         comodityData.Add(o14);
         comodityData.Add(o15);

         FileStream ts = new FileStream("comoditys.dat", FileMode.Open);
         BinaryFormatter tf = new BinaryFormatter();
         comodityData = (List<comodity>)tf.Deserialize(ts);
         ts.Close();

         if (comodityData != null)
         {
            if (comodityData.Count > 0)
               Alligor = comodityData[0].comodityPrice;

            if (comodityData.Count > 1)
               Briochit = comodityData[1].comodityPrice;

            if (comodityData.Count > 2)
               Chollonin = comodityData[2].comodityPrice;

            if (comodityData.Count > 3)
               Espitium = comodityData[3].comodityPrice;

            if (comodityData.Count > 4)
               Hydrobenol = comodityData[4].comodityPrice;

            if (comodityData.Count > 5)
               Isopropenetol = comodityData[5].comodityPrice;

            if (comodityData.Count > 6)
               Metachropin = comodityData[6].comodityPrice;

            if (comodityData.Count > 7)
               Phlobotil = comodityData[7].comodityPrice;

            if (comodityData.Count > 8)
               Plasteosine = comodityData[8].comodityPrice;

            if (comodityData.Count > 9)
               Polynitrocol = comodityData[9].comodityPrice;

            if (comodityData.Count > 10)
               Polynucleit = comodityData[10].comodityPrice;

            if (comodityData.Count > 11)
               Prilumium = comodityData[11].comodityPrice;

            if (comodityData.Count > 12)
               Statchanol = comodityData[12].comodityPrice;

            if (como开发者_Python百科dityData.Count > 13)
               Titanium = comodityData[13].comodityPrice;

            if (comodityData.Count > 14)
               Vitricyl = comodityData[14].comodityPrice;
         }
      }

      private void textBox1_TextChanged(object sender, EventArgs e)
      {
         // 1st text box input is float
         float tempFloat;

         if (float.TryParse(textBox1.Text, out tempFloat))
         {              
            AlligorInput = tempFloat;
         }
         else
            MessageBox.Show("uh oh");
      }

      private void textBox2_TextChanged(object sender, EventArgs e)
      {
         // 1st text box input is float
         float tempFloat;

         if (float.TryParse(textBox1.Text, out tempFloat))
         {      
            BriochitInput = tempFloat;
         }
         else
            MessageBox.Show("uh oh");
      }

    private void textBox3_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {                
            CholloninInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {                
            EspitiumInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox5_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {                
            HydrobenolInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox6_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {               
           IsopropenetolInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox7_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {                
            MetachropinInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox8_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {               
            PhlobotilInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox9_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {               
            PlasteosineInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox10_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {                
            PolynitrocolInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox11_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {                
           PolynucleitInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox12_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {
            PrilumiumInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox13_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {                
            StatchanolInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox14_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {                
           TitaniumInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void textBox15_TextChanged(object sender, EventArgs e)
    {
        // 1st text box input is float
        float tempFloat;
        if (float.TryParse(textBox1.Text, out tempFloat))
        {               
           VitricylInput = tempFloat;
        }
        else
            MessageBox.Show("uh oh");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        costofAlligor = Alligor * AlligorInput;
        costofBriochit = Briochit * BriochitInput;
        costofChollonin = Chollonin * CholloninInput;
        costofEspitium = Espitium * EspitiumInput;
        costofHydrobenol = Hydrobenol * HydrobenolInput;
        costofIsopropenetol = Isopropenetol * IsopropenetolInput;
        costofMetachropin = Metachropin * MetachropinInput;
        costofPhlobotil = Phlobotil * PhlobotilInput;
        costofPlasteosine = Plasteosine * PlasteosineInput;
        costofPolynitrocol = Polynitrocol * PolynitrocolInput;
        costofPolynucleit = Polynucleit * PolynucleitInput;
        costofPrilumium = Prilumium * PrilumiumInput;
        costofStatchanol = Statchanol * StatchanolInput;
        costofTitanium = Titanium * TitaniumInput;
        costofVitricyl = Vitricyl * VitricylInput;

        totalCost = costofAlligor + costofBriochit + costofChollonin + costofEspitium + costofHydrobenol + costofIsopropenetol + costofMetachropin + costofPhlobotil + costofPlasteosine + costofPolynitrocol + costofPolynucleit + costofPrilumium + costofStatchanol + costofTitanium + costofVitricyl;
    }

      private void textBox16_TextChanged(object sender, EventArgs e)
      {
         textBox13.Text = totalCost.ToString();
      }
   }
}


Define only one class comodity and do so outside of any other class.


The simple answer is that you cannot do this! In C# you cannot cast a List with one generic type to another.

A simple workaround to this problem is to use the Cast Linq extension method:

List<Person> personlist = new List<Person>() { ... };
List<object> objectList = personlist.Cast<object>();
0

精彩评论

暂无评论...
验证码 换一张
取 消