Re: Strange requirement of IConvertible from XML serializer
- From: "Vagif Abilov" <vagif@xxxxxxxxx>
- Date: Sat, 11 Jun 2005 18:05:21 +0200
Hi Jon,
I am trying to extract the essential part of the code that would reproduce
it. When I simply tried to past this code in a test application, it worked.
There must be something special to this assembly, and I am trying to figure
out what. Basically if I write 2 lines:
MyClass c = Convert.ChangeType(o, typeof(MyClass));
c = (MyClass)o;
then the first one succeeds as long add IConvertible implementation to
MyClass, but the second fails. And they both work if I paste the code into
another test app. This is simply driving me mad. So as soon as I manage to
make short by complete program that reproduces it, I will post it here.
Thanks
Vagif
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1d1513ad46f6bbfc98c2c8@xxxxxxxxxxxxxxxxxxxxxxx
> Vagif Abilov <vagif@xxxxxxxxx> wrote:
>> I am often using XML serialization to deserialize type values from XML
>> files. Recently I came across the exception thrown by Framework when I
>> try
>> to cast serialized class from object to a type. The following happens:
>>
>> 1. I read type using XmlSerializer into a generic "object" instance.
>> 2. I check in debugger that the type is correctly loaded. It is. I even
>> cast
>> it in in debugger Watch window to MyClass. Everything is fine.
>> 3. On a next code line there is an identical cast as in debugger window:
>> MyClass inst = (MyClass)obj. But when I try to step over this line, I get
>> an
>> InvalidCastException with message "Class must implement IConvertible".
>>
>> To narrow down the problem I simplified the class to contain only one
>> member
>> variable of an integer type. But still I get the invalid cast exception!
>>
>> To test the problem further I derived MyClass from IConvertible and
>> implement placeholders for necessary methods. Then .NET is happy, and it
>> calls them. This is really weird. Why a primitive class with a single
>> "int"
>> variable would every require IConvertible?!
>
> Could you post a short but complete program which demonstrates the
> problem?
>
> See http://www.pobox.com/~skeet/csharp/complete.html for details of
> what I mean by that.
>
> --
> Jon Skeet - <skeet@xxxxxxxxx>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: Strange requirement of IConvertible from XML serializer
- From: Jon Skeet [C# MVP]
- Re: Strange requirement of IConvertible from XML serializer
- References:
- Strange requirement of IConvertible from XML serializer
- From: Vagif Abilov
- Re: Strange requirement of IConvertible from XML serializer
- From: Jon Skeet [C# MVP]
- Strange requirement of IConvertible from XML serializer
- Prev by Date: Re: Strange requirement of IConvertible from XML serializer
- Next by Date: Re: Strange requirement of IConvertible from XML serializer
- Previous by thread: Re: Strange requirement of IConvertible from XML serializer
- Next by thread: Re: Strange requirement of IConvertible from XML serializer
- Index(es):
Relevant Pages
|