Click here to Skip to main content
13,299,495 members (69,098 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 30 Jan 2011

Generic Casting of .NET objects with a useful error message

, 30 Jan 2011
Rate this:
Please Sign up or sign in to vote.
Use an extension method that applies to any object to Cast to other types

Often in code, we need to cast an object of one type to another at run-time, for example:

Customer customer = myObject as Customer;
// Or...
int number = (int) myObject;

However, if we forget to add the neccessary code to check that the object is of the expected type to be converted, we end up with the following error message(s) that do not help to determine WHY the cast failed:

Specified cast is not valid.

Object reference not set to an instance of an object.


Use an extension method on the Object class to perform a cast on any object in code and at run-time be assured that casting the wrong type will give us a useful error message to help solve the problem. For example:

Customer customer = myObject.CastTo<customer>();

An invalid cast results in an InvalidCastException error message that tells us why a cast failed:

"Expected object 'Joe Bloggs' to be of type MyCompany.Customer. Actual type is MyCompany.Manager."

The code for the Cast extension method is as follows:

/// <summary>
/// Extension methods for the <c>object</c> class.
/// </summary>
/// <remarks>
/// Works only in .NET 3.5+
/// </remarks>
public static class ObjectExtensions
    /// <summary>
    /// Casts the supplied object to the value or reference type <c>T</c>, throwing an <c>InvalidCastException</c> with a useful message if the object is not of type <c>T</c>.
    /// </summary>
        /// <remarks>
        /// 'CastTo' name is used instead of simply 'Cast' to avoid clashes with LINQ extension methods with the same name.
        /// </remarks>
    /// <param name="input">Object to be cast.</param>
    /// <exception cref="InvalidCastException">Thrown if <paramref name="input"/> is not of type <c>T</c>.</exception>
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="input"/> is null.</exception>
    /// <returns>Object cast to of type <c>T</c>.</returns>
    public static T CastTo<T>(this object input)
        T output;
        if (input != null)
            if (input is T)
                output = (T) input;
                string message = String.Format("Expected object '{0}' to be of type {1}. Actual type is {2}", input.ToString(), typeof(T).Name, input.GetType().Name);
                throw new InvalidCastException(message);
            throw new ArgumentNullException("input");
        return output;


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
New Zealand New Zealand
TC is a software developer with a Degree in Information Systems and commercial experience ranging from C DLL's to N-Tier Web applications.

You may also be interested in...


Comments and Discussions

GeneralNot need to check for null Pin
sergio_ykz7-Feb-11 5:37
membersergio_ykz7-Feb-11 5:37 
GeneralRe: Not need to check for null Pin
T-C7-Feb-11 10:58
memberT-C7-Feb-11 10:58 
GeneralRe: Not need to check for null Pin
sergio_ykz7-Feb-11 11:17
membersergio_ykz7-Feb-11 11:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 31 Jan 2011
Article Copyright 2011 by T-C
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid