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
> 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...
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.
I serialize and deserialize from a file....
"Duggi" <DuggiSri...@gmail.com> wrote in message
news:d8b26805-50ae-4498...@v16g2000prc.googlegroups.com...
That is gre8 techie to know... Thanks for sharing
-Cnu
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
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
> [...]
>> 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 Marc. I think I got the zest of it.
-Cnu
Thanks for the reply Pete. I think I got the zest of it.