I do serialization via a serialization constructor like this:
private MyClass(SerializationInfo info, StreamingContext c)
{
try
{
MyIntVar = info.GetInt32("MyIntVar");
}
catch(Exception)
{
Trace.WriteLine("Exception occured! Setting default value.");
MyIntVar = 4711;
}
}
What I’m now trying to achieve is to trace the name and path of the file that is beeing serialized when this exception occures.
Something like:
if( c is file)
{
Trace.WriteLine("Don't bother, I proceed anyway, but maybe you should repair the file " + FilePath);
}
So I have two questions concerning this:
- How can I determine that the current serialization context is a file?
- How can I get the corresponding file name and path of this file?
The only way you can do that is if you’ve created the
StreamingContextyourself, and have made some additional information available via the.Contextproperty. For example:and then in your constructor or callback, access it:
Actually,
stringis pretty ambiguous – I would advise using your own custom context-type that can’t ever be confused for something else. For example: