Sako73
Sako73

Reputation: 10137

How to cast to a type in C#

Can anyone explain how to get this to work? I am passing in the type name, and "t" is being correctly populated. I just cannot figure out how to cast objectToCast to type "t". Any help is appreciated.

....
Type t = Type.GetType("castToTypeNameHere");
o = CastTo<t>(objectToCast);
....

private T CastTo<T>(Object obj)
{
    return (T)obj;
}

FYI, here's the answer I found:

Type t = Type.GetType(element.Attribute("castToType").Value);
MethodInfo castMethod = this.GetType().GetMethod("CastTo", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(t);
object castedObject = castMethod.Invoke(this, new object[] { objectToCast });

Upvotes: 3

Views: 4547

Answers (4)

David Ly
David Ly

Reputation: 31586

If you think the answer you said you found:

Type t = Type.GetType(element.Attribute("castToType").Value);
MethodInfo castMethod = this.GetType().GetMethod("CastTo", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(t);
object castedObject = castMethod.Invoke(this, new object[] { objectToCast });

actually does something, then you don't understand what casting means.

The compile time type is still object, so you've gained nothing. You can directly just cast anything as object directly.

Casting doesn't change the type of an object, it only tells the compiler that you know what type it is, and the compiler will believe you. If you're wrong an error will still happen, only it's at runtime with an InvalidCastException instead of at compile time.

The problem is that casting to a type that isn't know at compile time just doesn't make sense. Even if you could hypothetically, what would that give you?

Type someType = Type.GetType("castToTypeNameHere");
someType o = CastTo<someType>(objectToCast);

What methods does o have? someType could be anything, so there's no way the compiler can know what methods or properties it has, It would be exactly the same as

object o = (object)objectToCast

Because there's nothing you could do with someType that you couldn't with object.

Upvotes: 0

Austin Salonen
Austin Salonen

Reputation: 50215

Perhaps Convert.ChangeType would help you here.

Type t = Type.GetType("castToTypeNameHere");

//using dynamic
dynamic obj = Convert.ChangeType(objectToCast, t);
obj.SomeExpectedMethod(); 

//casting to known interface
var obj = Convert.ChangeType(objectToCast, t) as IKnowWhatImSupposedToBe;
if (obj == null) HandleBadState();

Upvotes: 1

Mark Cidade
Mark Cidade

Reputation: 99957

You can't use a variable for a generic type parameter (e.g., CastTo<t>)—it has to be the actual type name (e.g., CastTo<string>).

Upvotes: 1

jason
jason

Reputation: 241601

When you use generics (without reflection), the type parameters have to be the name of types, not instances of System.Type. So you can't say

Type t = Type.GetType("castToTypeNameHere");
o = CastTo<t>(objectToCast);

because t is not the name of a type. It's as if you had said

o = CastTo<typeof(int)>(objectToCast);

instead of

o = CastTo<int>(objectToCast);

The former is illegal, the latter is legal.

I don't understand what you're trying to achieve. If you don't know the type at compile time, a cast like that is useless. The compiler won't know the type of o, and you won't get any of the compile-time type safety nor IntelliSense features.

Upvotes: 8

Related Questions