Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Help me with some Generic casting awesomeness

I'm trying to write a method for coverting a given object to an instance of a given type. I started with this:

private static T TryCast<T>(object o)
{
    return (T) o;
}

Going in, I know that isn't going to work, but it illustrates the concept. Now, I'm going to start having problems when I have types that won't cast automatically, like string --> DateTime. I was trying to use the Convert Class to deal with these cases, but I just get a compile time error instead of a runtime error. The following code gets the compile error "Cannot cast expression of type 'string' to type 'T'

private static T TryCast<T>(object o)
{
    var typeName = typeof (T).FullName;

    switch (typeName)
    {
        case "System.String":
            return (T) Convert.ToString(o);
        default:
            return (T) o;
    }
}

I'm also aware of Convert.ChangeType(), but I'm wondering if it will handle edge cases that I would otherwise handle in the above switch, like the stated string --> DateTime that I'd normally just use Convert.ToDateTime for.

private static T TryCast<T>(object o)
{
    return (T)Convert.ChangeType(o, typeof(T));
}

So, what is my best option? If somebody can give me a workable approach, I can take it from there.

like image 825
bopapa_1979 Avatar asked Mar 01 '12 16:03

bopapa_1979


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

Convert.ChangeType should handle edge cases; it delegates to IConvertible.

To answer the question, the compiler doesn't know that T is string.
Therefore, it doesn't let you cast between to unrelated types (just like you can't cast Button to TextBox).

You can work around that by casting to object first:

return (T)(object)o.ToString();

Now, each individual conversion is allowed by the compiler (it's either a direct upcast or a direct downcast), and you know that the whole thing will work because T is String.

like image 144
SLaks Avatar answered Sep 30 '22 00:09

SLaks