Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type is not serializable because it's not public?

I have a public class that won't serialize properly. When attempted, the following exception is thrown:

The data contract type 'MyProject.MyClass' is not serializable because it is not public. Making the type public will fix this error. Alternatively, you can make it internal, and use the InternalsVisibleToAttribute attribute on your assembly in order to enable serialization of internal members - see documentation for more details. Be aware that doing so has certain security implications.

My class is public, though:

[DataContract]
public class MyClass
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    private int Count;

    public MyClass()
    {
        Name = string.Empty;
        Count = 0;
    }
}

Why am I getting this exception when the class is clearly public?

like image 527
brantonb Avatar asked Feb 14 '11 06:02

brantonb


People also ask

Why is Java Object not serializable?

In Java, a NotSerializableException exception is thrown when an instance of a class must implement the Serializable interface. The exception is thrown by either the serialization runtime, or by the instance of the class. The argument for the NotSerializableException is the name of the class.

How can you prevent a property from being serialized?

You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.

What does it mean to be serializable in Java?

To serialize an object means to convert its state to a byte stream so that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.

Why is serialization not good?

Serialization is brittle, it pokes into private field, violates constructor invariance, it's horrible in so many ways. The only thing appealing about it is that it's easy to use in simple use cases. That's what motivated getting it in there.


2 Answers

In Windows Phone 7 apps, you can't serialize private members:

Well, it just so happens that WP7 apps run in a “partial trust” environment and outside of “full trust”-land, DataContractSerializer refuses to serialize or deserialize non-public members. Of course that exception was swallowed up internally by .NET so all I ever saw was that bizarre message about things that I knew for certain were public being “not public”. I changed all the private fields I was serializing to public and everything worked just fine. http://geekswithblogs.net/mikebmcl/archive/2010/12/29/datacontractserializer-type-is-not-serializable-because-it-is-not-public.aspx

Changing the code to avoid serializing private members fixes the issue.

like image 156
brantonb Avatar answered Sep 28 '22 00:09

brantonb


You might be able to fix this by writing custom serialization for this class. The built-in XML serialization can only serialize public properties.

You can do one of the following to work around this limitation:

  • Get rid of the DataMember attribute on Count
  • Make Count public
  • Implement IXmlSerializable (or whatever other serialization you need to), and serialize those private members manually
like image 30
Merlyn Morgan-Graham Avatar answered Sep 28 '22 01:09

Merlyn Morgan-Graham