Hi, I'm hoping for a bit of advice on a design problem I have, in using XML
serialization:

What I want/need to do, is serialize a class which contains a reference of
type object.  The object reference however, will be to a class type which
is not known at compile time.  The class type will be defined in another
assembly.

I will have a set of registry entries which map a key name to the actual
class type, so that it should be possible to deserialize the appropriate
class by embedding the key name elsewhere in the xml element.

Example:

Assembly #1

 public class ReaderConnection
 {
  string m_readerType = string.Empty;
  string m_connectionName = string.Empty;
  object m_settings = null;

  public string ReaderType
  {
   get
   {
    return m_readerType;
   }
   set
   {
    m_readerType = value;
   }
  }

  public string ConnectionName
  {
   get
   {
    return m_connectionName;
   }
   set
   {
    m_connectionName = value;
   }
  }

  public object Settings
  {
   get
   {
    return m_settings;
   }
   set
   {
    m_settings = value;
   }
  }
 }

Assembly #2

 public class MyReaderSettings
 {
  string m_ipAddress = string.Empty;
  int m_portNumber = 0;

  public string IpAddress
  {
   get
   {
    return m_ipAddress;
   }
   set
   {
    m_ipAddress = value;
   }
  }

  public int PortNumber
  {
   get
   {
    return m_portNumber;
   }
   set
   {
    m_portNumber = value;
   }
  }
 }


So, in this example, what I would like to do would be to be able to
serialize the ReaderConnection class from Assembly #1.  However, it's
Settings property will refer to an instance of MyReaderSettings from
Assembly #2, which cannot be known at compile time for Assembly #1.

What I would like to do would be to keep in the registry a set of entries
which map the ReaderType key name to a fully qualified class name for the
actual class stored (in this case MyReaderSettings).

So, the encoded XML would look something like:

<ReaderConnection Name="MyConnection" ReaderType="MyReaderSettings">
   <Settings>
      <IpAddress>127.0.0.1</IpAddress>
      <PortNumber>8080</PortNumber>
   </Settings>
</ReaderConnection>

When serializing the ReaderConnection class, the associated key name or
ReaderType string would be written as an XML element (or attribute as in
the example), the class instance referred to, should then have it's
settings also serialized in the Settings element.

When deserializing the ReaderConnection class, the key name or ReaderType
string would be read, then when attempting to deserialize the Settings
element, the fully qualified class name for the class associated with the
ReaderType would be read from the registry and the system would create the
class and request it to deserialize it's properties from the remaining
elements.

My question, is: Is this possible using XML serialization, and if so, does
anyone have any advice on how to achieve this ?

I have been unable to find a way of serializing an unknown polymorphic type
and dynamically intercepting the serialization/deserialization process.

Thanks in advance
Phil Parker.

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
You may be interested in Guerrilla .NET, 24 March 2003, in London
http://www.develop.com/courses/gdotnet

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to