Select to view content in your preferred language

Error: Specific cast is not valid

1403
7
12-06-2011 11:31 AM
EhsanRoshani
Emerging Contributor
Hi Everyone,

I have a problem with the following code I would be appreciated if anyone could help.

Assume I have a following data structure and I would like to serialize it and then deserialize it.
both functions work well except for the red line in the deserialize funciotn

[Serializable()]
public struct Test
{
public double a;
public double b;
}

public void Serialize(Test obj)
{
Stream file = File.Open(@"c:\tt.dat", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(file, obj);
file.Close();
}

This is the deserializing function

public Test DeSerialize(string Name)
{
Test Obj = new Test();

try
{
Stream file = File.Open(Name, FileMode.Open);
BinaryFormatter b = new BinaryFormatter();
Obj = (Test)b.Deserialize(file); //This is the line which propagate the exception
file.Close();
}
catch (Exception)
{
}

return Obj;
}

in the red line I get this error code

"Specific cast is not valid"

if instead of Test struct I use a simple data type, let say double, it work without any problem but as soon as I use any kind of structure it gives me this error.

Is there any reason why it happen?

Thanks

Ehsan
0 Kudos
7 Replies
NeilClemmons
Honored Contributor
The error means the object being deserialized isn't of the same type as your struct.  Are you sure the file path that you're passing into the Deserialize method is "c:\tt.dat"?  That's about the only thing I can think of is that you're not opening the file you think you are.
0 Kudos
EhsanRoshani
Emerging Contributor
Yup! files name and path are the same
the weird thing is that if I use a simple data type it works fine.
Thanks for your response.
e
0 Kudos
NeilClemmons
Honored Contributor
I copied your code verbatim into a form class and ran it without any errors.  Here's the code for the entire form - it includes the struct definition as well.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Serialize(Test obj)
        {
            Stream file = File.Open(@"c:\tt.dat", FileMode.Create);
            BinaryFormatter b = new BinaryFormatter();
            b.Serialize(file, obj);
            file.Close();
        }

        public Test DeSerialize(string Name)
        {
            Test Obj = new Test();

            try
            {
                Stream file = File.Open(Name, FileMode.Open);
                BinaryFormatter b = new BinaryFormatter();
                Obj = (Test)b.Deserialize(file); //This is the line which propagate the exception
                file.Close();
            }
            catch (Exception)
            {
            }

            return Obj;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Test o = new Test();
            o.a = 44;
            o.b = 55;
            Serialize(o);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Test o = DeSerialize(@"c:\tt.dat");
            MessageBox.Show(o.a.ToString());
            MessageBox.Show(o.b.ToString());
        }
    }
}

[Serializable()]
public struct Test
{
public double a;
public double b;
}
0 Kudos
EhsanRoshani
Emerging Contributor
Neil,

You are right it works in a windows form project but if you put it in ArcMap Add In project it does not work and it shows the error.

Sorry I forgot to mention that it should be an ArcMap Add In project.

Thanks for your time and prompt response

E
0 Kudos
NeilClemmons
Honored Contributor
I don't develop addins as they aren't a good solution for what our applications do so I'm not up to speed on all their ins and outs.  However, I know there are certain limitations with them.  Have you tried serializing to another location (like maybe the same directory where your addin is located)?  There could be some limitation on what parts of the file system they can fully access.  I don't know, just a guess since the problem doesn't appear to be the code itself.
0 Kudos
EhsanRoshani
Emerging Contributor
Hi Neil,

Sorry for delaying response I was out of town.

I will try your suggestion and let you know the results.

Thanks
Ehsan
0 Kudos
EhsanRoshani
Emerging Contributor
Neil,

Unfortunately, It did not work.

Any suggestions would be appreciated

E
0 Kudos