locked
Type in assembly is not marked as serializable RRS feed

  • Question

  • I have an activity class that I want to be able to serialize. This class contains fields like 'path' which takes an assembly(.dll) as input. So, here I don't have access to the code to the assembly that I feed as input. I need to be able to use this assembly to connect to some of the functionalities provided by third party sw. But whenever I try to serialize the class object, it leaves me with exception like below...

    Type...in Assembly.. , Version=4.1.0.0., Cutlure=neutral, PublicKeyToken=null' is not marked as serializable.

    Please, guide me as to how I could resolve this issue.

    Thanks in advance!


    Thursday, September 22, 2016 4:35 PM

Answers

  • Show some details about the definition and initialisation of path fields. Maybe you do not have to serialize these fields. Then mark them with [NonSerialized] attribute.

    Thursday, September 22, 2016 6:11 PM
  • Hi jessica2030,

    Thank you for posting here.

    For your question, if a field or property on your object is the type of your problem, like Viorel said to mark as Nonserialized.

    Or you could create a derivate of the type which is marked as Serializable like following.

    [Serializable()]		
    public class TestSimpleObject  {}

    If you derive from the type and mark it as Serialized, you will most likely have to create the serialization logic yourself. This requires you to implement ISerializable to allow object to control its own serialization and deserialization.

    Here is a simple code in MSDN article demonstrates the use of the ISerializable interface to define custom serialization behavior for a class.

    using System;
    using System.Text;
    using System.IO;
    // Add references to Soap and Binary formatters.
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization.Formatters.Soap ;
    using System.Runtime.Serialization;
    
    
    [Serializable]
    public class MyItemType : ISerializable
    {
        public MyItemType()
        {
            // Empty constructor required to compile.
        }
    
        // The value to serialize.
        private string myProperty_value;
    
        public string MyProperty
        {
            get { return myProperty_value; }
            set { myProperty_value = value; }
        }
    
        // Implement this method to serialize data. The method is called 
        // on serialization.
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            // Use the AddValue method to specify serialized values.
            info.AddValue("props", myProperty_value, typeof(string));
    
        }
    
        // The special constructor is used to deserialize values.
        public MyItemType(SerializationInfo info, StreamingContext context)
        {
            // Reset the property value using the GetValue method.
            myProperty_value = (string) info.GetValue("props", typeof(string));
        }
    }
    
    // This is a console application. 
    public static class Test
    {
        static void Main()
        {
            // This is the name of the file holding the data. You can use any file extension you like.
            string fileName = "dataStuff.myData";
    
            // Use a BinaryFormatter or SoapFormatter.
            IFormatter formatter = new BinaryFormatter();
            //IFormatter formatter = new SoapFormatter();
    
            Test.SerializeItem(fileName, formatter); // Serialize an instance of the class.
            Test.DeserializeItem(fileName, formatter); // Deserialize the instance.
            Console.WriteLine("Done");
            Console.ReadLine();
        }
    
        public static void SerializeItem(string fileName, IFormatter formatter)
        {
            // Create an instance of the type and serialize it.
            MyItemType t = new MyItemType();
            t.MyProperty = "Hello World";
    
            FileStream s = new FileStream(fileName , FileMode.Create);
            formatter.Serialize(s, t);            
            s.Close();
        }
    
    
        public static void DeserializeItem(string fileName, IFormatter formatter)
        {
            FileStream s = new FileStream(fileName, FileMode.Open);
            MyItemType t = (MyItemType)formatter.Deserialize(s);
            Console.WriteLine(t.MyProperty);            
        }       
    }

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 23, 2016 7:31 AM

All replies

  • Show some details about the definition and initialisation of path fields. Maybe you do not have to serialize these fields. Then mark them with [NonSerialized] attribute.

    Thursday, September 22, 2016 6:11 PM
  • Hi jessica2030,

    Thank you for posting here.

    For your question, if a field or property on your object is the type of your problem, like Viorel said to mark as Nonserialized.

    Or you could create a derivate of the type which is marked as Serializable like following.

    [Serializable()]		
    public class TestSimpleObject  {}

    If you derive from the type and mark it as Serialized, you will most likely have to create the serialization logic yourself. This requires you to implement ISerializable to allow object to control its own serialization and deserialization.

    Here is a simple code in MSDN article demonstrates the use of the ISerializable interface to define custom serialization behavior for a class.

    using System;
    using System.Text;
    using System.IO;
    // Add references to Soap and Binary formatters.
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization.Formatters.Soap ;
    using System.Runtime.Serialization;
    
    
    [Serializable]
    public class MyItemType : ISerializable
    {
        public MyItemType()
        {
            // Empty constructor required to compile.
        }
    
        // The value to serialize.
        private string myProperty_value;
    
        public string MyProperty
        {
            get { return myProperty_value; }
            set { myProperty_value = value; }
        }
    
        // Implement this method to serialize data. The method is called 
        // on serialization.
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            // Use the AddValue method to specify serialized values.
            info.AddValue("props", myProperty_value, typeof(string));
    
        }
    
        // The special constructor is used to deserialize values.
        public MyItemType(SerializationInfo info, StreamingContext context)
        {
            // Reset the property value using the GetValue method.
            myProperty_value = (string) info.GetValue("props", typeof(string));
        }
    }
    
    // This is a console application. 
    public static class Test
    {
        static void Main()
        {
            // This is the name of the file holding the data. You can use any file extension you like.
            string fileName = "dataStuff.myData";
    
            // Use a BinaryFormatter or SoapFormatter.
            IFormatter formatter = new BinaryFormatter();
            //IFormatter formatter = new SoapFormatter();
    
            Test.SerializeItem(fileName, formatter); // Serialize an instance of the class.
            Test.DeserializeItem(fileName, formatter); // Deserialize the instance.
            Console.WriteLine("Done");
            Console.ReadLine();
        }
    
        public static void SerializeItem(string fileName, IFormatter formatter)
        {
            // Create an instance of the type and serialize it.
            MyItemType t = new MyItemType();
            t.MyProperty = "Hello World";
    
            FileStream s = new FileStream(fileName , FileMode.Create);
            formatter.Serialize(s, t);            
            s.Close();
        }
    
    
        public static void DeserializeItem(string fileName, IFormatter formatter)
        {
            FileStream s = new FileStream(fileName, FileMode.Open);
            MyItemType t = (MyItemType)formatter.Deserialize(s);
            Console.WriteLine(t.MyProperty);            
        }       
    }

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 23, 2016 7:31 AM