array, xmlserializer and inheritance

  • Thread starter Thread starter Smola
  • Start date Start date
S

Smola

Hi all,


I have a situation like this:


abstract class A
{
public ArrayList List;
// This list will contain objects of derived classes - like B
}

class B : A
{

}


I need to serialize class B with XmlSerializer but in the class A i
cannot define what type will go in the List (with XmlArrayItem) because
the class is abstract and I don't know what types can be derived from
it.

How can I define this in the class B? Is this approach ok:

class B : A
{
[XmlArray, XmlArrayItem (Type = typeof(B))
new public ArrayList List;
}



Thanks.
 
Hello Smola,

Im not sure your object model makes sense...

You want a class to maintain a list of instances of that class? How do you
know which one is the main class? This is very similar to the Registry pattern
from PoEAA, and in that pattern you have a separate class that handles the
registry of objects.

Once you do that, the serialization should just fall into place...
 
Hello Smola,

Im not sure your object model makes sense...

You want a class to maintain a list of instances of that class?

I want to have an object of the class Node and a list of its child nodes
in the ArrayList, ok?



Hi all,

I have a situation like this:

abstract class A
{
public ArrayList List;
// This list will contain objects of derived classes - like B
}
class B : A
{
}

I need to serialize class B with XmlSerializer but in the class A i
cannot define what type will go in the List (with XmlArrayItem)
because the class is abstract and I don't know what types can be
derived from it.

How can I define this in the class B? Is this approach ok:

class B : A
{
[XmlArray, XmlArrayItem (Type = typeof(B))
new public ArrayList List;
}
Thanks.
 
Smola said:
Hello Smola,

Im not sure your object model makes sense...

You want a class to maintain a list of instances of that class?

I want to have an object of the class Node and a list of its child nodes
in the ArrayList, ok?



Hi all,

I have a situation like this:

abstract class A
{
public ArrayList List;
// This list will contain objects of derived classes - like B
}
class B : A
{
}

I need to serialize class B with XmlSerializer but in the class A i
cannot define what type will go in the List (with XmlArrayItem)
because the class is abstract and I don't know what types can be
derived from it.

How can I define this in the class B? Is this approach ok:

class B : A
{
[XmlArray, XmlArrayItem (Type = typeof(B))
new public ArrayList List;
}
Thanks.
Perfecly reasonable, and I don't know the solution either.
Thanks
 
Back
Top