.NET: How to determine if a variable can be converted to a particular type

Platform: .NET Framework
Language: C#

Using reflection, we can determine if a particular variable can be converted to another data type.  This is useful if you're not sure if the conversion that you're about to perform is valid or not.  Although this can be done using the common way of converting, for example:

      int x = 10;
   Convert.ToDateTime(x);


However, the previous code will throw an exception, so you're obligated to enclose the code in the "try...catch" block.  Exceptions are, however, expensive operations and can be avoided in cases of invalid conversions.  By changing the above code to:

       using System.ComponentModel;
       ...
       ...
       ...

       int x = 10;

       IConvertible c = x as IConvertible;
       if (c == null)
       {
             // code here
       }

       if (TypeDescriptor.GetConverter(x).CanConvertTo(typeof(System.DateTime)) == false)
       {
             // code for invalid conversion here
       }

you can avoid using exceptions just to handle invalid conversions.

I know what you're thinking, it takes several lines more than the "common" way.... but you can encapsulate all this code into a conversion function, and using the above code would become more "convenient".  One thing to note though, the "CanConvertTo" returns false when checking if a string variable can be converted to a boolean type.  So special handling should be done within the function for such a case.

I've attached a simple console app demonstrating this.  Also, take note of the "IConvertible.ToType" conversion function in the attachment.  Very useful, IMHO.

For further information, refer to the MSDN documentation on the TypeDescriptor, TypeConverter classes and the IConvertible interface.

HYFTU

Attachment: Conversion.zip
Published Monday, November 13, 2006 7:04 PM by raistlin
Filed under: , ,

Comments

# re: .NET: How to determine if a variable can be converted to a particular type

I'd use TypeConverters for complex types but normally, conversions are just strings to other data types wherein i'd rather use regular expressions

Tuesday, November 14, 2006 2:46 AM by jokiz

# re: .NET: How to determine if a variable can be converted to a particular type

I agree with you jokiz. I think this would be more applicable for cases wherein you don't know beforehand all the possible conversions that you're going to make and you want to make a general conversion routine.

Tuesday, November 14, 2006 3:51 AM by raistlin

# re: .NET: How to determine if a variable can be converted to a particular type

So far I have not exeperienced thse situations and Im pretty contented with IS operator ;).

if(x is y){

 z = x as y;

}

nice post!

Tuesday, November 14, 2006 4:28 AM by dehran ph

# re: .NET: How to determine if a variable can be converted to a particular type

Thanks dehran. If you're dealing strictly with reference types, i'd like to suggest an alternative.

You could write the code as

SomeType z = x as SomeType;

if (z != null)

{

 // valid conversion

}

This way you just check for nullity, which is less expensive than using the "is" operator.  Of course, in the world of multi-core processors, the performance gain is negligible. :)

Thanks again.

Tuesday, November 14, 2006 6:45 PM by raistlin

# re: .NET: How to determine if a variable can be converted to a particular type

nice post raistlin :)

I agree with your approach. less code clutter. :)

Thursday, November 16, 2006 11:31 AM by keithrull

# re: .NET: How to determine if a variable can be converted to a particular type

thanks keith... i really appreciate the compliment. :)

Thursday, November 16, 2006 11:32 PM by raistlin

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: