Re: Strange requirement of IConvertible from XML serializer

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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


.



Relevant Pages

  • Re: Strange requirement of IConvertible from XML serializer
    ... Recently I came across the exception thrown by Framework when I try ... > to cast serialized class from object to a type. ... I check in debugger that the type is correctly loaded. ... > it in in debugger Watch window to MyClass. ...
    (microsoft.public.dotnet.framework.clr)
  • Re: A simple cast.
    ... you can cast it to PVOID or only store it under the correct pointer type ... >> I have a class MyClass, and I have instantiated it in the variable ... > CoolFunction *is not virtual*, you can use the following syntax... ... > Anti-spam: my real e-mail address has no digits; ...
    (microsoft.public.win32.programmer.kernel)
  • autoexp.dat and expression evaluation
    ... class Impl; // Impl defined elsewhere ... These classes are implemented in their own DLL ... we try and look at the value of any of these classes in the debugger ... For example, given an instance of MyClass called myClass, we ...
    (microsoft.public.vc.debugger)
  • Re: Unique GUID for an asynchronous task
    ... I am using SendAsync; userToken is the second argument of SendAsync. ... myClass mailid = e.UserState; ... you simply cast the UserState property back to the appropriate type. ... If you have a more complex data structure, you pass an instance to your data structure and then cast back to the data structure type. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# equivalent to TryCast
    ... double cast ... static void Main(stringargs) ... B myclass = new B; ...
    (microsoft.public.dotnet.languages.csharp)