Answered by:
Inconsistent type conversion

Question
-
Just found some inconsistency in type conversion of the .NET types. For example: you cannot do:
long l = (long)((object)2);
but you can do:TypeCode tc = (TypeCode)((object)3);
In the contrary, Convert.ChangeType behaves just the opposite: you can do:
Convert.ChangeType(2, typeof(long));
but you cannot do:
Convert.ChangeType(2, typeof(TypeCode));
I am posting this because I was writing a ITypeConverter inplementation, one of whose functionality is to convert an integer value to a enum, whose actual type is passed in as the targetType parameter. It is frustrating to have to use something nasty like the following:
private static T Cast<T>(object o) { return (T)o; }
Visual Studio 2008 rocks!Wednesday, November 25, 2009 6:48 PM
Answers
-
Hello George
My original problem was to look for a straight forward way to convert an integer to a enum, in which case the type of the enum is specified by a Type parameter. I did find some code snipets from the net and they worked, but non of them are really straight forward. I do not want to work with IL code for this simple task, either.
Please try out the Enum.ToObject method.
Type t = typeof(MyEnum);
object r = Enum.ToObject(t, 1);
It's able to convert the value to a corresponding enum object.
Regards,
Jialiang Ge
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked as answer by jialge_msftMicrosoft employee Thursday, December 10, 2009 11:08 AM
Tuesday, December 1, 2009 10:33 AM
All replies
-
I recommend reading Eric Lippert's post on this issue. He delves into detail about some of these issues.
That being said, the problem wtih your first "casts" is that you aren't actually just casting. When you do (object)2, you're boxing the Int32 value (2) into an object. You can't unbox and cast in one operation, so your cast to long fails. It's not because it's a cast - it's because it's unboxing and casting.
As for the TypeCode examples - You should actually be using GetTypeCode, but in your case, this is probably not going to help you much anyways.
If you're trying to work with Enum values using generics, there are some tricks that make life easier. However, they're not all possible directly in C#. I'd recommend checking out UnconstrainedMelody, which is Jon Skeet's project on using generics with enum values. There are many useful features in there, including conversion routines similar to what you're attempting, but which aren't possible in C# directly. Jon Skeet used post-compilation code rewriting to do things in IL that aren't possible directly in C#. He blogged about it, as well.
Reed Copsey, Jr. - http://reedcopsey.com- Proposed as answer by Geert van Horrik Monday, November 30, 2009 3:53 PM
Wednesday, November 25, 2009 7:35 PM -
I picked TypeCode just as an example of some enum - I do not want to create a enum type just for demonstration.
As with doing unbox and cast in one operation, the code TypeCode tc = (TypeCode)((object)3) is just the same, and this one works, while the other one does not.
My original problem was to look for a straight forward way to convert an integer to a enum, in which case the type of the enum is specified by a Type parameter. I did find some code snipets from the net and they worked, but non of them are really straight forward. I do not want to work with IL code for this simple task, either.
Visual Studio 2008 rocks!Wednesday, November 25, 2009 8:10 PM -
Hello George
My original problem was to look for a straight forward way to convert an integer to a enum, in which case the type of the enum is specified by a Type parameter. I did find some code snipets from the net and they worked, but non of them are really straight forward. I do not want to work with IL code for this simple task, either.
Please try out the Enum.ToObject method.
Type t = typeof(MyEnum);
object r = Enum.ToObject(t, 1);
It's able to convert the value to a corresponding enum object.
Regards,
Jialiang Ge
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked as answer by jialge_msftMicrosoft employee Thursday, December 10, 2009 11:08 AM
Tuesday, December 1, 2009 10:33 AM -
Hello
How are you? Does the above suggestion help you? If you have any other questions, please feel free to post here.
Regards,
Jialiang Ge
MSDN Subscriber Support in Forum
If you have any feedback of our support, please contact msdnmg@microsoft.com.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Monday, December 7, 2009 4:16 AM