Hi,

I want to serialze an object. I use the binary formatter because it's a very complex object. I solved a lot of SerializationExceptions, but there are two that I don't understand or know how to handle.

It says

"The type "Ivi.Visa.Interop.FormattedIO488Class" in assembly "Ivi.Visa.Interop, Version=3.2.0.0, Culture=neutral, PublicKeyToken=a128c98f1d7717c1" is not marked as serializable."

The problem is that Ivi.Visa.Interop is just a using directive in some of my classes. I don't know how to make them serializable.

The second problem is a warning

"The referenced assembly "ZedGraph.Web" could not be resolved because it has a dependency on "System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" which is not in the currently targeted framework ".NETFramework,Version=v4.0,Profile=Client". Please remove references to assemblies not in the targeted framework or consider retargeting your project."

This is just a reference. I don't know what to do :(

Thanks for help.

I tried something else but there's an error, too. Here is my code:

[Serializable()]    
public abstract class Measurement : ISerializable
{

    public Measurement(SerializationInfo info, StreamingContext ctxt)
    {
        //Get the values from info and assign them to the appropriate properties
        SelectedInnerSweep = (String)info.GetValue("InnerSweep", typeof(int));
        SelectedOuterSweep = (String)info.GetValue("OuterSweep", typeof(string));
    }

    //Serialization function.
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
       //You can use any custom name for your name-value pair. But make sure you
      // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
     // then you should read the same with "EmployeeId"
     info.AddValue("InnerSweep", SelectedInnerSweep);
     info.AddValue("OuterSweep", SelectedOuterSweep);
   }

}

public partial class mainWindow : Form
{
        /* Variables for Serializing a  measurement object */
        static BinaryFormatter binFormatter = new BinaryFormatter();
        static FileStream myStream;

        // Serialize object 
        public static void SerializeObject(Measurement obj)
        {
            myStream = new FileStream(@"C:\Dokumente und Einstellungen\labor\Desktop\Testobjekt.dat", FileMode.Create);
            MessageBox.Show("Writing Measurement Information");
            binFormatter.Serialize(myStream, obj);            
            myStream.Close();            
        }

        // Deserialize object 
        public object DeserializeObject()
        {            
            myStream = new FileStream(@"C:\Dokumente und Einstellungen\labor\Desktop\Testobjekt.dat", FileMode.Open, FileAccess.Read);
            try
            {

                return binFormatter.Deserialize(myStream);
            }

            /*catch (TargetInvocationException target)
            {
                MessageBox.Show("Fehler: " + target.Message);
                return null;
            }*/
            finally
            {
                myStream.Close();
            }           

        }

        private void testDeserialize()
        {
            selectedMeasurement = null;
            selectedMeasurement = (Measurement)DeserializeObject();
            MessageBox.Show("Reading Measurement Information");
            String oldObjType = selectedMeasurement.GetType().Name;
            MessageBox.Show(oldObjType);
            String oldObjName = selectedMeasurement.Name;
            switch (oldObjType)
            {
                case "DC_Measurement":
                    MessageBox.Show(String.Format("SelectedInnerSweep: {0} \nSelectedOuterSweep: {1}",
                        selectedMeasurement.SelectedInnerSweep, selectedMeasurement.SelectedOuterSweep));
                    break;                
                default:
                    break;
            }

        }

}

When I call the testDeserialize() method after I serialized the measurement object, I get following exception:

"TragetInvocationException was unhandled by user code."

It would be great, if you can help me with both problems.

Thanks.

I found the error in the second post. In the constructor Measurement() it should say (string) instead of (int).

That happens by using copy and paste -.-

Hi tortura,

Im facing the same error as well. Have you resolved it?

Your help is highly appreciated,TQ.

Unfortunately, if the object you want to serialize isn't marked as serializable and you can't make it such, then you can't automatically serialise it the way you're trying.

However! You can serialise it yourself manually, you just need to remember to deserialise it at the other end.

A good way to do this, is to create a dedicated "Data Transfer Object". Use this object solely as a transport to get specific data from A to B.

Note: There is no library for Data Transfer Objects, it's just a name :)

You can go about manual serialisation in many ways, the easiest and probably least efficient way is to make it a delimited string.

Name,Bob,Age,25,Job,Builder

Then rebuild your object at the destination.

Example:

[Serializable]
public class EmployeeDTO
{
    // This variable represents a 'Person' class
    public String Person
    { get; set; }

    // This variable represents a 'Job' class
    public String Job
    { get; set; }

    public static EmployeeDTO Serialise(Person person, Job job)
    {
        EmployeeDTO dto = new EmployeeDTO();
        StringBuilder sb = new StringBuilder();
        sb.Append("Name,");
        sb.Append(person.Name);
        sb.Append("Age,");
        sb.Append(person.Age);

        dto.Person = sb.ToString();

        /* Continue to serialise Person and then serialise Job */

        ...Much code later...

        return dto;
    }
}

Like I said, this isn't the best way to do it, but it is A way to do it :)

Now you have the basic methodology you can improve it somewhat by organising data and making byte arrays and such forth.

Hope this helps.

Thanks for help but I solved the problem in another way. Aqeela, you said, that you have the same problem.

I got a hint to assign null to the variable "FormattedIO488" while serializing. Because this variable is just needed to access the devices. BUT you have to create this variable when you are deserializing the object. I hope that you understand me.

Now I tried something what I did a few weeks ago. I just marked this variable as [NonSerialized()] and everything works. Without assigning null and without creating the variable again.

I hope this helps you.

It's pretty bad practise to simply serialize everything in a class, whether related to the data you want or not. Not only can it be highly inefficient, you can run into issues with 3rd party libs that don't play nicely with the .net object binary serializer (especially interops). Using a Data To Send class, as mentioned here, is definately the better route as it gives you maximum control over the serialization process (dont use CSV for it though, lol!)

This article has been dead for over six months. Start a new discussion instead.