I am trying to serialize a class that is made up of many variables all inheriting from IMyDataType
.
public abstract class IMyDataType<T>
{
protected virtual T Data { get; set; }
public abstract String ToString(String args = null);
public abstract Boolean SetValue(T newValue);
public abstract Boolean CheckValue(T newValue);
public abstract T GetValue();
}
For instance I might have a class MyInteger
public class MyInteger : IMyDataType<int>
{
public int Min { get; protected set; }
public int Max { get; protected set; }
protected override int Data { get; set; }
public MyInteger(int value)
{
Min = int.MinValue;
Max = int.MaxValue;
if (!SetValue(value))
Data = 0;
}
public MyInteger(int min, int value, int max)
{
Min = min;
Max = max;
if (!SetValue(value))
Data = 0;
}
public override Boolean SetValue(int newVal)
{
if (newVal >= Min && newVal <= Max)
{
Data = newVal;
return true;
}
return false;
}
public override Boolean CheckValue(int newVal)
{
return (newVal >= Min && newVal <= Max);
}
public override int GetValue() { return Data; }
public override String ToString(String args = null) { return Data.ToString(args); }
}
Whenever I try to serialize a subclass of IMyDataType
the variable T Data
is never serialized, though Min
and Max
are. What do I need to do to get T Data
to be serialized?
EDIT: Based on DotNetom's answer I changed the access modifier of T Data
to public, which did allow it to be serialized. Though, there are reasons for it being protected. Is there any other means by which I could serialize it while keeping its access modifier intact?