0

I am using serializable, but cannot leave my private class.

[DataContract]
public class test
{
    [DataMember]
    public String name { get; set; }
}

what problems in leave this class in public.

I do not understand this encapsulation, because I can't use it this way.

[DataContract]
public class test
{
    [DataMember]
    private int myVar;

    public int MyProperty
    {
        get { return myVar; }
        set { myVar = value; }
    }
}
Jashaszun
  • 9,207
  • 3
  • 29
  • 57
vrbsm
  • 1,188
  • 15
  • 22
  • 2
    Why not? What happens if you try? – nvoigt Aug 05 '14 at 15:32
  • this error is shown in log - The data contract type 'Project.Model.Test' cannot be serialized because the member 'myVar' is not public. Making the member 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. – – vrbsm Aug 05 '14 at 17:38
  • if (System.Diagnostics.Debugger.IsAttached) { Debug.WriteLine("EX MESSAGE " + e.ExceptionObject.Message); // An unandled exception has occurred; break into the debugger System.Diagnostics.Debugger.Break(); } – vrbsm Aug 05 '14 at 17:39
  • I did not downvote. Sometimes there's people that downvote without leaving a comment. I don't like it either, but that's how this site works. Votes are anonymous and you don't need to give a reason. – nvoigt Aug 05 '14 at 20:35

1 Answers1

0

A data contract is something public. A contract between two parties. The other party may not even have the same internal representation as you have. It will work as long as the public contract is fulfilled.

If you want to serialize the object yourself, you can use other ways, for example implementing IXmlSerializable for XmlSerialization.

nvoigt
  • 75,013
  • 26
  • 93
  • 142