Serialization of non-Serialized collection

  • Thread starter Thread starter Tamir Khason
  • Start date Start date
T

Tamir Khason

I have collection based class wich is non serializable (for some reason)
[NonSerClass]
I want to create another class with similar to [NonSerClass], but
serializable [SerClass]
so doing:

public class NonSerClass:CollectionBase
{Whatever}

[Serializable]
public class SerClass : NonSerClass

This working fine, except the little problem. while casting from NonSerClass
to SerClass I recieve "Invalid Cast" exception, as well as attempts like
NonSerClass no_ser_foo = new NonSerClass();
SerClass ser_foo =no_ser_foo as SerClass;
returns null

Please advice. Why is it

TNX
 
Tamir,

The reason that this doesn't work is that you can not cast a reference
up when the type is not that of the derived type. no_ser_foo is just a
reference to an instance of NonSerClass. If you want an instance of
SerClass, you have to create it yourself.

Hope this helps.
 
Back
Top