IsSerializable

  • Thread starter Thread starter INeedADip
  • Start date Start date
I

INeedADip

Is there a way to check and see if an object is serializable without just
attempting it and catching the error?
 
INeedADip,

Unfortunately, not easily. If an object is serializable, it will have
the attribute on it (which is really a bit set in the metadata, as opposed
to other attributes). Unfortunately, this doesn't mean that it will
serialize correctly, since the compiler doesn't do a check on all of the
fields of the object being passed in the call (nor can it, really, without
extensive static analysis, which it is beyond the scope of the compiler to
perform).

So in the end, the only option you have is to try and serialize the
instance, and catch the exception.

Hope this helps.
 
Hello Nicholas Paldino [.NET/C# MVP],

And what if to perform check for constrains (like public methods and fields)
dynamically by CodeDom?

N> So in the end, the only option you have is to try and serialize
N> the instance, and catch the exception.---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Michael,

Well, that's hard to do, when you consider generic types. You basically
have to make sure that each field is serializable. Of course, in order to
do that, you have to make sure that each field in objects referenced in the
class are serializable, and so on, and so on.


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

Michael Nemtsev said:
Hello Nicholas Paldino [.NET/C# MVP],

And what if to perform check for constrains (like public methods and
fields) dynamically by CodeDom?

N> So in the end, the only option you have is to try and serialize
N> the instance, and catch the exception.---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do
not cease to be insipid." (c) Friedrich Nietzsche
 
Back
Top