I have custom class with following properties:
Class Person
readonly public string Name;
readonly public string FamilyName;
readonly public string UserName;
private List<Person> Team = new List<Person>();
public Person Leader { get; private set; }
public bool HasTeam { get; private set; }
I am getting error on serializer because "Object has Leader property that has no public set." However I need to keep it private, as change of Leader will cause errors. Do you know any way around? Or I need to make it public and keep in mind that I cannot set it?
Thank you, Michael
So, as mentioned, you could make it so, that it's it can be set only once, but i wouldn't see it as a good option (maybe you should rather rethink how you would like to store this information?)
public class Person
{
private Person leader;
public Person Leader
{
get
{
return leader;
}
set
{
if (Object.Equals(leader, value))
{
return;
}
if (leader != null)
{
throw new InvalidOperationException("Leader can be set only once!");
}
leader = value;
}
}
}
this would allow you to save/load the values, and it wouldn't allow it to be set afterwards. However, this is just working around the problem.
In case you don't have to save it specifically to XML, you could use a binary formatter, that saves the entire Person object (no matter if it contains private fields / properties)