So, I want to create an exception that is serializable and sealed.
I have a WizardBaseException that inherits ApplicationException:
[Serializable()]
public class WizardBaseException:ApplicationException
{
// code omitted
}
I provide a constructor for deserialization where I initialize all my member variables:
protected WizardBaseException(SerializationInfo info, StreamingContext context):
base(info, context)
{
Initialize();
assemblyName = info.GetString(assemblyNameKey);
utcDateTime = info.GetDateTime(utcDateTimeKey);
appDomainName = info.GetString(appDomainNameKey);
currentProcessId = info.GetString(currentProcessIdKey);
currentThreadId = info.GetString(currentThreadIdKey);
currentThreadUser = info.GetString(currentThreadUserKey);
machineName = info.GetString(machineNameKey);
}
I also override GetObjectData to serialize all the members into the streaming context:
public override void GetObjectData(SerializationInfo info
, StreamingContext context)
{
info.AddValue(appDomainNameKey, appDomainName, StringType);
info.AddValue(assemblyNameKey, assemblyName, StringType);
info.AddValue(currentProcessIdKey,currentProcessId,IntegerType);
info.AddValue(currentThreadIdKey, currentThreadId, IntegerType);
info.AddValue(currentThreadUserKey, CurrentThreadUser, StringType);
info.AddValue(machineNameKey, machineName, StringType);
info.AddValue(utcDateTimeKey, utcDateTime, DateTimeType);
base.GetObjectData (info, context);
}
This works great. Here is the kicker, when I create a concrete custom exception that is not inheritable:
[Serializable]
public sealed class InvalidWizardId:WizardBaseException
{//.... code omitted}
and I include the protected constructor so that the runtime can deserialize my exception like so:
protected InvalidWizardId(SerializationInfo info, StreamingContext context):
base(info,context)
{
}
I get the following compiler warning:
'member' : new protected member declared in sealed class
A sealed class cannot introduce a protected member because no other class will be able to inherit from the sealed class and use the protected member.
The following sample generates CS0628:
// CS0628.cs
// compile with: /W:4
This makes sense sort of, but I thought the framework used the constructor to deserialize the exception. Is this warning going to cause an exception somewhere down the line that I am not aware of or can this simply be ignored.
In the end, what really needs to happen is that during deserialization the members of InvalidWizardId need to be populated using the serialization info, where else could this be done other than the protected constructor?
I suppose I could just make InvalidWizardId not sealed and let other developers worry about it, but a quick sanity check would be nice.