Errors "trying to serialize" an object

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

In a application I have serveral User Controls and some of them inherit from
one in particular.

From time to time, when VS wants, an error appears (for example, now I'm
writting this post and it just appear without doing anything in VS). This
error message says that the code for a property could't been generated and
it says that a class wasn't marked as serializable. I'm not using anything
serializable at all in my app.

Do you know how can I solve this? Thank you.
 
Alberto said:
In a application I have serveral User Controls and some of them inherit
from one in particular.

From time to time, when VS wants, an error appears (for example, now
I'm writting this post and it just appear without doing anything in VS).
This error message says that the code for a property could't been
generated and it says that a class wasn't marked as serializable. I'm
not using anything serializable at all in my app.

Do you know how can I solve this?

No, not if you're not going to post any code showing the problem, nor
any specific information about the error, such as the exact error text
and the context in which it occurs (compile, run time, where it's
displayed etc.).

Pete
 
Alberto said:
In a application I have serveral User Controls and some of them inherit
from one in particular.

From time to time, when VS wants, an error appears (for example, now
I'm writting this post and it just appear without doing anything in VS).
This error message says that the code for a property could't been
generated and it says that a class wasn't marked as serializable. I'm
not using anything serializable at all in my app.

Do you know how can I solve this? Thank you.

Have you tried marking your property [NotSerialized], or making the
class serializable? You may not need serialization, but Visual Studio does.
 
I tried marking the property as serializable but the compiled told me that I
only can mark classes and structs (and I don't remember if anything else) so
I marked the class as serializable but the error appear again.
Thank you.

Family Tree Mike said:
Alberto said:
In a application I have serveral User Controls and some of them inherit
from one in particular.

From time to time, when VS wants, an error appears (for example, now I'm
writting this post and it just appear without doing anything in VS). This
error message says that the code for a property could't been generated
and it says that a class wasn't marked as serializable. I'm not using
anything serializable at all in my app.

Do you know how can I solve this? Thank you.

Have you tried marking your property [NotSerialized], or making the class
serializable? You may not need serialization, but Visual Studio does.
 
Alberto said:
I tried marking the property as serializable but the compiled told me
that I only can mark classes and structs (and I don't remember if
anything else) so I marked the class as serializable but the error
appear again.
Thank you.

I meant mark the property NonSerializable, as below:

class Foo
{
[NonSerialized]
public string Name { get; set; }
public Foo() { }
}

As Pete said, an example from you that we can reproduce here would help
us to help you.
 
I tried to reproduce the error in a new project. I created a new Windows
Form Project and added this class:

public partial class Nodo : Label
{
#region Campos
private List<Nodo> hijos = new List<Nodo>();
private List<Nodo> padres = new List<Nodo>();
#endregion

#region Propiedades
public List<Nodo> Hijos
{
get { return hijos; }
set { hijos = value; }
}

public List<Nodo> Padres
{
get { return padres; }
set { padres = value; }
}
#endregion

#region Constructores
public Nodo()
{
InitializeComponent();
}

public Nodo(string Texto)
{
InitializeComponent();

this.Text = Texto;
}
#endregion
}

Just add this class. If you try to compile, the app runs fine (you can see
the form and there are no errors or warnings) but after try to add the
control "Nodo" to the form, you will see the errors.

Thank you.
 
Alberto said:
I tried to reproduce the error in a new project. I created a new Windows
Form Project and added this class:

public partial class Nodo : Label
{
#region Campos
private List<Nodo> hijos = new List<Nodo>();
private List<Nodo> padres = new List<Nodo>();
#endregion

#region Propiedades
public List<Nodo> Hijos
{
get { return hijos; }
set { hijos = value; }
}

public List<Nodo> Padres
{
get { return padres; }
set { padres = value; }
}
#endregion

#region Constructores
public Nodo()
{
InitializeComponent();
}

public Nodo(string Texto)
{
InitializeComponent();

this.Text = Texto;
}
#endregion
}

Just add this class. If you try to compile, the app runs fine (you can
see the form and there are no errors or warnings) but after try to add
the control "Nodo" to the form, you will see the errors.

Thank you.

Are you starting with a UserControl, and changing it to inherit from
Label again? Label does not have a InitializeComponent method.
 
In a first aproach, I created serveral userControls and then I changed
UserControl for Label but, as somebody told me in this news, I did it again
creating a class and made it inherit from Label but probably I fogot do it
in this class.

Anyway, I repeated the example after deleting the InitializeComponent()
method but the error is still there.

Sorry, but I don't have idea about how to solve it. Thank you.
 
Alberto said:
In a first aproach, I created serveral userControls and then I changed
UserControl for Label but, as somebody told me in this news, I did it again
creating a class and made it inherit from Label but probably I fogot do it
in this class.

Anyway, I repeated the example after deleting the InitializeComponent()
method but the error is still there.

Sorry, but I don't have idea about how to solve it. Thank you.

Maybe Mike's figured it out, but I for one am still not clear on what
problem it is exactly you're having. The code you've posted so far
isn't helpful, as it doesn't compile as-is, never mind does it allow for
a test.

If you want a good answer, you need to take the advice that's already
been given to you with respect to how to ask a good question.

Pete
 
If you delete the "partial" declaration of the class and the
InitializeComponent() call, it will compile.

The app will compile but if you try to insert this control in a form you
will see the error.
Thank you.
 
Alberto said:
If you delete the "partial" declaration of the class and the
InitializeComponent() call, it will compile.

The app will compile but if you try to insert this control in a form you
will see the error.

And to clarify to other readers of this thread: the error is displayed
as an alert, presented to the user at the moment the control is added to
the form.

You should have provided a concise-but-complete code example, but I was
able to reproduce the problem using the incomplete example you offered.

As for the error, it's telling you exactly what it looks like it's
telling you: your object has a property that can't be serialized.

As Mike said, the Designer requires that public properties for a Control
class used in the designer be serializable. For simple properties, such
as strings, ints, etc. the Designer can handle those fine. But it can't
automatically generate initialization code for more complex properties,
such as "List<Nodo>".

You have two choices: disable serialization for those properties, or
support serialization for those properties. What you _can't_ do is fail
to disable serialization for those properties, but also fail to provide
serialization support for those properties. In that case, the Designer
will attempt to serialize the properties, but fail because you haven't
provided the necessary support.

If you want to learn about support serialization, you can read the MSDN
documentation on ISerializable and/or the "[Serializable]" code attribute.

If you simply want to disable serialization for those properties, add
the "[DesignerSerializationVisibility]" attribute, with the "Hidden"
option, so that the Designer will know to ignore those properties (of
course, any initialization of those properties will have to be done
explicitly in your own code). For example:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public List<Nodo> Hijos
{
get { return hijos; }
set { hijos = value; }
}

(Note that the "[NonSerialized]" attribute Mike mentioned is for fields
only...for properties, you need the above).

Personally, those properties seem suspect to me design-wise anyway.
Allowing client code direct access to a complex data structure defining
relationships between that instance and others is generally a bad idea.
So it may be a better way to fix the problem is simply to not expose
those lists in that way in the first place.

But if you feel you must, you need to correctly deal with the Designer's
dependencies on the properties.

Pete
 
Back
Top