Type Casting between Base and Derived Type (Serialization of objects with Type members) 
Author Message
 Type Casting between Base and Derived Type (Serialization of objects with Type members)

To all:

I started with the problem of trying to serialize an object that contained a
member of type Type.  After finding out that a Type object can not be
serialized,  I tried another approach to simplifying the class object to be
serialized.  My solution was to create a base class with Type descriptive
information, and a derived class containing the actual Type member.  The
thought being that I can serialize the base class, and re-establish the Type
object through reflection.

This all works fine, except that I have trouble with the casting between the
Base and Derived Types.  When casting from the derived type to the Base
type, the base type object keeps the type identity of the derived type.
When casting from the Base Type to the Derived Type, I receive a cast error.

I have overcome these problems by implementing brute force memberwise copy
methods, but I'm looking for something more elegant (and easier to
maintain).  BTW, I tried the MemberwiseClone method, but this maintains the
type information.

For illustration, I have included an example of the class structures
discussed:

public class TypeDefinition : TypeDescription
{
    public Type ControlType;   // Type of control

Quote:
};

public class TypeDescription
{
    public bool blnObject;
    public string strObject;
    public int intObject;

    public TypeDescription CopyDescription()
    {
       TypeDescription objTypeDescription = new TypeDescription();

        objTypeDescription .blnObject = this.blnObject;
        objTypeDescription .strObject = this.strObject ;
        objTypeDescription .intObject = this.intObject;
        return(objTypeDescription );
    }

Quote:
};

Now assuming that I have created TypeDefinition Object and successfully
serialized the base class of these objects, I encounter problems when trying
to read-in these serialized objects, as I try and cast the serialized
objects back to a TypeDefintion type object.

TypeDescription objDesc = new TypeDescription ();
TypeDefinition objDef = new TypeDefinition ();
objDesc = (TypeDescription )serializer.Deserialize(XmlReader);        //
Read Description structure
objDef = objDesc;                                // Crashes on this line;
illegal cast

Any assistance is appreciated.

Mike Casey



Sat, 20 Mar 2004 05:25:15 GMT  
 Type Casting between Base and Derived Type (Serialization of objects with Type members)
Inline ...


Quote:
> To all:

> I started with the problem of trying to serialize an object that contained
a
> member of type Type.  After finding out that a Type object can not be
> serialized,  I tried another approach to simplifying the class object to
be
> serialized.  My solution was to create a base class with Type descriptive
> information, and a derived class containing the actual Type member.  The
> thought being that I can serialize the base class, and re-establish the
Type
> object through reflection.

> This all works fine, except that I have trouble with the casting between
the
> Base and Derived Types.  When casting from the derived type to the Base
> type, the base type object keeps the type identity of the derived type.
> When casting from the Base Type to the Derived Type, I receive a cast
error.

> I have overcome these problems by implementing brute force memberwise copy
> methods, but I'm looking for something more elegant (and easier to
> maintain).  BTW, I tried the MemberwiseClone method, but this maintains
the
> type information.

> For illustration, I have included an example of the class structures
> discussed:

> public class TypeDefinition : TypeDescription
> {
>     public Type ControlType;   // Type of control
> };

> public class TypeDescription
> {
>     public bool blnObject;
>     public string strObject;
>     public int intObject;

>     public TypeDescription CopyDescription()
>     {
>        TypeDescription objTypeDescription = new TypeDescription();

>         objTypeDescription .blnObject = this.blnObject;
>         objTypeDescription .strObject = this.strObject ;
>         objTypeDescription .intObject = this.intObject;
>         return(objTypeDescription );
>     }
> };

> Now assuming that I have created TypeDefinition Object and successfully
> serialized the base class of these objects, I encounter problems when
trying
> to read-in these serialized objects, as I try and cast the serialized
> objects back to a TypeDefintion type object.

> TypeDescription objDesc = new TypeDescription ();
> TypeDefinition objDef = new TypeDefinition ();
> objDesc = (TypeDescription )serializer.Deserialize(XmlReader);        //

What kind of object have you serialized? Is it a TypeDescription or
TypeDefinition? If you have serialized a TypeDescription, do not expect to
cast it to TypeDefinition!

Quote:
> Read Description structure
> objDef = objDesc;                                // Crashes on this line;
> illegal cast

> Any assistance is appreciated.

> Mike Casey

And your implementation is not the correct way to do things! Your class
should implement the ISerializable interface and handle the serialization /
materialization in a custom way. That way you can serialize a string
representation of the Type instance and transform the string to a Type when
materializing.


Sat, 20 Mar 2004 06:12:06 GMT  
 Type Casting between Base and Derived Type (Serialization of objects with Type members)

Todor,

Thanks for the tip.

One thing that I neglected to emphasize is that I'm trying to serialize the data structure to an XML file.  As a result, I have been trying to work with the System.Xml.Serialization namespace to accomplish this task.  Unfortunately, the documentation for this namespace is very limited, and I have not been able to find any good examples of how to override the serialization for a member of type "Type."

Perhaps I can make use of the SoapFormatter class to accomplish this, but again the documentation is not very helpful.  If you know of any examples for serializing objects with complex data types into XML, please post back.

Note: The separation of base and derived classes is not really the issue, I'd prefer to just have a single class.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Sat, 20 Mar 2004 20:46:28 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Why can't overload type cast to base type (object)

2. type casting to / from pointers to incomplete data types

3. type casting to / from pointers to incomplete data types

4. cast cstring type to integer type

5. Casting to/from STL types to MFC types

6. C# Base Types vs. .NET Types

7. Variables and types of type "Type"

8. Object type cannot be converted to target type

9. ATL data type for VB Object data Type

10. Casting to Derived type

11. Casting a object instance to a unknown type.

12. Templates based on basic data types used inside ATL object

 

 
Powered by phpBB® Forum Software