Property must only be set using Serializer

I have a class that interacts with the API and needs to do some transformations for any data that it affects. This class is also equivalent:

public class SerializeMe
{
    public SerializeMe(string someString)
    {
        _someString = someString;
    }

    private string _someString;
    public string TransformedValue
    {
        get { _someString = TransformToSomething();
              return _someString; }
        set { _someString = value; }
    }
}

For my API users, I log each request and response by serializing these classes. They act like xml schemes almost.

Now everything works fine, my only problem is that someone could theoretically try to install and not return the expected results. This is mainly a design issue that is simply trying to make my code accountable. I would like to be able to use a private set, but the XmlSerializer complains about this.

- , Transformed unsettable, ?

, , . ?

+3
3

XmlSerializer, DataContractSerialzer ( ctor, XmlSerializer). [DataContract] ( ) [DataMember].

[DataContract]
public class SerializeMe
{
    public SerializeMe(string someString)
    {
        _someString = someString;
    }
    [DataMember]
    private string _someString;
    public string TransformedValue
    {
        get { _someString = TransformToSomething();
              return _someString; }
        private set { _someString = value; }
    }
}
+2

XmlSerializer , , , IXmlSerializable. XmlSerializer , IXmlSerializable, , . , , , -.

+2

, XmlSerializer getters/seters (, , IXmlSerializable).

, ( .NET , ), :

public class SerializeMe
{
    private string _someString;
    public string SomeString
    {
        get
        {
            _someString = TransformToSomething();
            return _someString;
        }
        set { }
    }

    public void SetString(string val) { _someString = val; }
}

Obviously, IXmlSerializablethis is more work, but ultimately this is by far the best solution (not to mention that this is not a hack).

0
source

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


All Articles