XmlSerializer

D

Duggi

Hi

In the following code why it is required to type cast while de-
serialize... any ideas???

to create an object of XmlSerializer, Type is a compalsary parameter.
Atleast by default XmlSerializer should convert it to the Type it is
made for...

class MyClass
{
//Some implementation
}

class Program
{

static void Main(string[] args)
{
MyClass cs;
XmlSerializer s = new XmlSerializer(typeof(MyClass));
FileStream fs = new FileStream("serializedobject.xml",
FileMode.Open);

cs = (MyClass) s.Deserialize(fs);


Console.ReadLine();
}



-Cnu
 
D

Duggi

Check here for some ideas/samples:http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry

I serialize and deserialize from a file....


In the following code why it is required to type cast while de-
serialize... any ideas???
to create an object of XmlSerializer, Type is a compalsary parameter.
Atleast by default XmlSerializer should convert it to the Type it is
made for...
class MyClass
{
       //Some implementation
}
class Program
{
       static void Main(string[] args)
       {
           MyClass cs;
           XmlSerializer s = new XmlSerializer(typeof(MyClass));
           FileStream fs = new FileStream("serializedobject.xml",
FileMode.Open);
           cs = (MyClass) s.Deserialize(fs);
           Console.ReadLine();
       }

That is gre8 techie to know... Thanks for sharing

-Cnu
 
D

Duggi

You seem to be confusing the issue of the type as the compiler knows it  
with the issue of the type as the run-time knows it.

You need to cast because the _compiler_ doesn't know the type.  But the 
cast doesn't do anything to the object at all.  The serializer _does_  
create an instance of the desired type, as you expect.  All the cast does  
is tell the compiler what type it is, so that you can use the instance as 
the actual type rather than Object (*).

If XmlSerializer was a generic type, then it would have been possible for 
the Deserialize() method to simply return instance as the correct type.  
But the class pre-dates generics and so only the casting approach was  
possible when the class was designed.

Pete

(*) I've oversimplified casting a bit.  The run-time actually checks the  
type and will throw an exception if it's wrong.  But, from a practical  
point of view, the main purpose for casting is to translate for the  
compiler how a given instance is perceived.  It doesn't change the  
instance itself in any way.

Pete,
Thanks for the reply.
You need to cast because the _compiler_ doesn't know the type.

I did not know it. I was under the impression that ... While creating
an object (s) it is required to specify the Type(MyClass) of object
for which serialization object (s) is created. So logically, compiler
should know that when ever "s" is used it has to deserialize to
"MyClass".

This is what exactly my confusion was.

Thanks
-Cnu
 
M

Marc Gravell

To do this it would have to use generics i.e. it would be
XmlSerializer<Foo>, not new XmlSerializer(typeof(Foo)).

In theory this would be quite nice, but a: XmlSerializer pre-dates
generics (it exsisted with 1.1), and b: it would be [slightly] harder
to use in certain scenarios where the type isn't known (you'd have to
use MakeGenericType etc, although presumably this could have been
wrapped).

The first seems a stronger reason, which makes me wonder why
DataContractSerializer isn't generic... we may never know ;-p But you
only need to cast, so it isn't really much of an issue.

Marc
 
D

Duggi

To do this it would have to use generics i.e. it would be
XmlSerializer<Foo>, not new XmlSerializer(typeof(Foo)).

In theory this would be quite nice, but a: XmlSerializer pre-dates
generics (it exsisted with 1.1), and b: it would be [slightly] harder
to use in certain scenarios where the type isn't known (you'd have to
use MakeGenericType etc, although presumably this could have been
wrapped).

The first seems a stronger reason, which makes me wonder why
DataContractSerializer isn't generic... we may never know ;-p But you
only need to cast, so it isn't really much of an issue.

Marc

Thanks for the reply Marc. I think I got the zest of it.

-Cnu
 
D

Duggi

[...]
You need to cast because the _compiler_ doesn't know the type.
I did not know it. I was under the impression that ... While creating
an object (s) it is required to specify the Type(MyClass) of object
for which serialization object (s) is created. So logically, compiler
should know that when ever "s" is used it has to deserialize to
"MyClass".

The compiler doesn't know that.  All it knows is that "typeof(MyClass)" 
was passed to the constructor of XmlSerializer.  It has no idea what  
XmlSerializer is going to do with that parameter.  So, no...it's not true  
that the "compiler should know...it has to deserialize to 'MyClass'".  The  
compiler's not even aware that deserialization is what's happening.  All  
it knows is that you've created an object by passing in a specific  
parameter, and then called a method on that object.  It has no specific 
knowledge of what's actually going on.

Pete

Thanks for the reply Pete. I think I got the zest of it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top