Using DataContractAttribute and Serializable

I have a set of user types that already implement the ISerializable interface, now I would like to place them in the server-side application and expose some of these types to the client, marking them with the DataContract attribute.

Unfortunately, when I cannot mark one class with the DataContract attribute, because it is already ISerializable and throws a runtime exception.

But at the same time, I cannot remove the ISerializable implementation in the old user type.

Someone will help me expose these types to the client .. by marking the DataContract and without removing ISerializable

Thanks Sandeep

+3
source share
2 answers

- : DataContract , ISerializable, ISerializable , DataContract

?

/

  [DataContract]
class Person 
{
    public Person()
    {

    }
    public Person(string firstName, string lastName):this()
    {
        this.FirstName = firstName;
        this.LastName = LastName;
    }

    [DataMember]
    public string FirstName {get;set;}

    [DataMember]
    public string LastName { get; set; }

}

   [Serializable]
    class SerializablePersonWrapper : ISerializable 
    {
       SerializablePersonWrapper(SerializationInfo info,
                                 StreamingContext context)
        {
            string fname = info.GetString("FName");

            //did this just for answering any questions
            string lname = (string) info.GetValue("LName", typeof(string)); 

            this.PersonItem = new Person(fname, lname);

        }
       public Person PersonItem {get;set;}

       public void GetObjectData(SerializationInfo info, 
                                 StreamingContext context)
       {
           info.AddValue("FName", this.PersonItem.FirstName);
           info.AddValue("LName", this.PersonItem.LastName);
       }
    }
+1

Source: https://habr.com/ru/post/1794028/


All Articles