WARNING for users of WCF partial classes
If you're trying to add a property to a WCF proxy class (generated by Add Service Reference) you might be surprised to find that private fields aren't initialized because apparently no constructor at all is called.
If you attempt to do this (as suggested in some other answers) it won't ever get called :
private bool _sendEmail = true;
This has nothing to do with whether the field is in a partial class or not.
What you have to do is add an [OnDeserialized] attribute which lets you do further initialization to the object. This is part of System.Runtime.Serialization so is only useful in the context of serialization when using DataContractSerializer.
public partial class EndOfDayPackageInfo
{
[OnDeserialized()]
public void Init(StreamingContext context)
{
_sendEmail = true;
}
private bool _sendEmail;
public bool SendEmail
{
get
{
return _sendEmail;
}
set
{
_sendEmail = value;
RaisePropertyChanged("SendEmail");
}
}
}
Another approach is to 'lazy load' the property - but this approach is much less elegant.
private bool _sendEmail;
private bool _sendEmailInitialized;
public bool SendEmail
{
get
{
if (!_sendEmailInitialized)
{
_sendEmailInitialized = true;
_sendEmail = true; // default value
}
return _sendEmail;
}
set
{
if (!_sendEmailInitialized)
{
// prevent unwanted initialization if 'set' is called before 'get'
_sendEmailInitialized = true;
}
_sendEmail = value;
RaisePropertyChanged("SendEmail");
}
}