Keep getting exception message that Type is not marked as Serializable

Hello,

I a pretty sure that I am on the right track.

I am trying to serialize a object which has a multitude of member variables. Some are collections of graphics based objects ie objects which are drawn in a view (TabPage derived).

The object to the serialized also is drawn in such a view. It is like a parent object that contains other objects.

I have separated code from data so that I am not trying to serialize anything like the TabPage view or Pens, Brushes....

Despite this, I am continually getting an exception when trying to serialize the object.

I am being told that a view of mine OpeningsView is not marked as serializable.

The paradox is that I have not asked that is to be serialized (it's a view!!!)

I notice that when I put in try catch code that something does get sent to the test file despite the exception being thrown

I realise that without providing any code, it is difficult to comment on this, however any comments would be welcome.

Under a ton of pressure.

Thanks

Andrew.



Answer this question

Keep getting exception message that Type is not marked as Serializable

  • Arun R

    David,

    Firstly, may I say that I really appreciated being able to 'talk' to someone from the VC # team. The help was quick and concise.

    The problem was that my objects had events that were connected via a delegate to methods that exist in View classes.

    I followed your tip and used the following attribute when declaring the events in the graphics objects i.e.

    [field:NonSerialized]

    I read through a really useful lnk as well, in order to develop my understanding i.e.

    http://msdn.microsoft.com/msdnmag/issues/04/10/AdvancedSerialization/

    I did implement the IDeserializableCallback interface in each of my graphics classes and their I created things like GraphicsPaths and Matricies.

    I am now able to successfully serialize and deserialize objects!!!!!!

    There is a problem with clicking on restored support objects like Dimensions (which should respond with a options box), however I will further investigate this before asking any more questions.

    May I ask a general question

    I noticed in the above url, it mentions that .NET Framework v2.0 supports event serialization support eg OnDeserializing, OnDeserialized...

    How do I upgrade from my current version which is v1.1 of the Framework

    I purchased a licence of Visual Studio .NET Professional about 18months ago.

    Much appreciated

    Andrew.


  • OckhamRazor

    How have you indicated that the OpeningsView object shouldn't be serialized Is it a member field of an object that you are trying to serialize

    What type of serialization are you using



  • Nirmala

    Hi David,

    This is already in place and yet the problem persists. The project has come to a complete standstill because of this serialization problem.

    Let me explain very briefly the main point.

    I have set up a test serialize situation where I try to serialize a Column object. (Very similar to the test situation that involves the OpeningsView problem)

    The Column class does NOT have any member variable that is of type ColumnsView.

    Column is part of a class hierarchy of abstract classes and NONE of them have any members of ColumnsView. One does have a member variable of LayerView which is the ColumnsView parent and that member variable is set to [NonSerialized].

    I go to set up a new design which creates several Column objects and I then click a toolbar button that serializes one of the Column objects.

    As soon as I try to do this, an exception occurs which indicates that the ColumnsView is not marked as serializable. NO KIDDING, I have not indicated anywhere that it is to be serialized !!!!!!!!!!!!!

    This seems ridiculous and I have one suspicion that I would like to ask you about ie

    In my class definition I connect an event of the Column class to a member function that actually exists in the ColumnsView class. The delegate is in the Column class.

    Could this be causing the problem. i.e when the Column class is serialized a member of the ColumnsView is involved (however maybe not as the event connect involves a static situation).

    Any comments would be really appreciated from you or anyone from Microsoft or other....

    This situation is critical.

    Thanks

    Andrew.


  • superstar

    The Serializer will try to serialize every field. If that field's type is not marked serializable, then an exception will be thrown.

    To mark a field as not serializable, use the NonSerializedAttribute, for example:



    [Serializable]
    public class MyClass
    {
        [NotSerialized]
        private OpeningsView _View;
    }

     



  • KristofferSjoo

    So did that solve your problem

  • M_Rajesh

    David,

     

    I will send two C# code files and the exception output to the email address given in your profile page.

    Andrew.


  • pic_az

    Andrew,

    Can you post an example code of what you are experiencing Code speaks a thousand words... ;)



  • RSantana

    Thanks David,

    I am using binary serialization ie BinaryFormatter().

    My definition of the OpeningsView is along the lines :

    For any view type class I indicate that the class is not to be serialized by simply omitting the [Serializable] property.

    public class OpeningsView : TabPage

    {

    }

    I specify the [Serializable] attribute for any object that I intend to serialise.

    For example I have several collections of objects that are graphical in nature. One such collection is an ArrayList of wall openings ie OpeningsList which contains an array of objects named Window, Door..... The Window object (for example) is drawn as a simple rectangle, with a specific color and position in the view that generated that object ie OpeningsView. The color is dependant on the view in which the object appears eg in the FloorView the wall openings are drawn in grey.

    In order for the OpeningsList to be serialized I define it as so :

    [Serializable]

    public class OpeningsList : ArrayList

    {

    }

    Similarly for any of the wall opening objects eg

    [Serializable]

    public class Window : WindowBase

    {

    }

    Yes the WindowBase is itself declared as Serializable !

    Each wall opening class has members which are themselves graphical in nature eg Rulers and Dimensions and these are indeed marked as Serializable.

    Each LayerView populates a collection of objects for its layer class eg Openings View populates a collection of wall opening objects for housed in the Openings layer.

    There are several layers each with it's own view.

    Hence the idea is to save a building design by serializing the collections of each layer, then to deserialize these collections when opening a saved existing building design (from disk).

    Now as mentioned I definitely do not want to serialize any code like the OpeningView hence I do not mark it as Serializable.

    However somehow, and even when I have not created any wall opening objects, the exception occurs.

    Andrew.


  • Chango V. - MSFT

    Thanks,

    Yes I am fully aware of that concept.

    Andrew.


  • Keep getting exception message that Type is not marked as Serializable