Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8790561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:41:23+00:00 2026-06-13T22:41:23+00:00

I do serialization via a serialization constructor like this: private MyClass(SerializationInfo info, StreamingContext c)

  • 0

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:

  1. How can I determine that the current serialization context is a file?
  2. How can I get the corresponding file name and path of this file?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:41:24+00:00Added an answer on June 13, 2026 at 10:41 pm

    The only way you can do that is if you’ve created the StreamingContext yourself, and have made some additional information available via the .Context property. For example:

    var ctx = new StreamingContext(StreamingContextStates.File, "SomeFileName");
    //                                                          ^^^^ = context
    var serializer = new BinaryFormatter(null, ctx);
    // then use serializer.Serialize / .Deserialize
    

    and then in your constructor or callback, access it:

    bool isFile = (c.State & StreamingContextStates.File) != 0;
    
    string filename = c.Context as string;
    if(filename != null) {
       // ...
    }
    

    Actually, string is pretty ambiguous – I would advise using your own custom context-type that can’t ever be confused for something else. For example:

    var ctx = new StreamingContext(StreamingContextStates.File,
        new MyStreamingContext { File = "SomeFile" });
    ...
    class MyStreamingContext {
        public string File {get;set;}
    }
    ...
    var context = c.Context as MyStreamingContext;
    if(context != null) {
        string file = context.File;
        // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a class which implements it's own (de)serialization via XLINQ, and I'd like
I use Boost.Serialization to serialize a std::map. The code looks like this void Dictionary::serialize(std::string
I have a client and server communicating via Spring remoting (using Java Serialization) over
I'm using native/C++/Win32/MFC code on Windows to save a document file via MFC serialization.
I'm trying to build XmlDocument so that after serialization I could achieve something like
I very much like the simplicity of calling remote methods via Java's RMI, but
Which kind of serialization is the most meaningful when sending serialized protobuf data via
in my Silverlight 4 application, I save my object via DataContractSerializer XML-Serialization - which
I have tried so many times to get this to compile, what I'd like
I found this link on serialization protocols , but only XML handles referencing of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.