Why is there no contains/exists in SerializationInfo?

  • Thread starter Thread starter KWienhold
  • Start date Start date
K

KWienhold

I have been working with serialization in some places for a while and
have now begun to run into version issues.
Unfortunately my company is still working with .Net 1.1, so using the
VTS of 2.0 was not an option.
So I decided to have my classes implement ISerializable and wrote a
set of functions that use reflection and mimick version tolerant
serialization.
Unfortunately the only way to determine wether a field exists in a
SerializationInfo is to try to access it and catch the
SerializationException if it occurs.
Because of the nature of try/catching, this process can be rather slow
(especially when an old version is lacking many of the expected
fields), so I have been wondering why there is no way to determine
beforehand wether a certain field exists within a SerializationInfo.
I tried finding an answer on google, but to no avail, so I'm just
wondering wether any of you guys know?

Sincerely,
Kevin Wienhold
 
After looking into the SerializationInfo class a bit more, I have
found a way to implement my own exists method, using the enumerator
supplied by GetEnumerator() to loop through the entries until I find
the one I want, however that still doesn't explain why this isn't
included in the class in the first place.
 
I agree, there should be some sort of method that will tell you if the
item exists for the current scope.

Using the GetEnumerator, you can eliminate the overhead of the
exceptions. However, I hope you are not calling GetEnumerator for each
member in your class, as this would most definitely cause a performance
problem as well.

Rather, instead of cycling through the members of your class using
reflection, why not call GetEnumerator and let that loop drive your process?
Then, you can call GetField on the Type instance of your class to see if the
field exists (and mark it if it doesn't). This way, you only have to loop
through the SerializationInfo once.

Hope this helps.
 
Nicholas Paldino said:
I agree, there should be some sort of method that will tell you if the
item exists for the current scope.

Using the GetEnumerator, you can eliminate the overhead of the
exceptions. However, I hope you are not calling GetEnumerator for each
member in your class, as this would most definitely cause a performance
problem as well.

Rather, instead of cycling through the members of your class using
reflection, why not call GetEnumerator and let that loop drive your
process? Then, you can call GetField on the Type instance of your class to
see if the field exists (and mark it if it doesn't). This way, you only
have to loop through the SerializationInfo once.

Or iterate once, storing into a Hashtable or Dictionary. Then you can use
the dictionary's own optimized Contains implementation.
Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

KWienhold said:
After looking into the SerializationInfo class a bit more, I have
found a way to implement my own exists method, using the enumerator
supplied by GetEnumerator() to loop through the entries until I find
the one I want, however that still doesn't explain why this isn't
included in the class in the first place.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top