Make compiler treat an object variable as if it was of type X

Hey everyone,
I’ve got a problem here that already had me trying for hours:
I have an input which is an object variable (For certain environmental reasons it really cannot be anything but an object), but in reality it’s of the type FsmPath .

Now I need to access some public attributes which do not exist in the object variable.
Eg, my input is “OutputPath”

(…)
[ObjectType(typeof(FsmPathfinding.FsmPath))]
public object OutputPath;
(…)

    FsmPath doo = (FsmPath)OutputPath;

This is the only way I found to cast it from an object to a FsmPath. If I do not do this it gives me errors if I try to access attributes. However, if I do it like this it also gives me errors, because it has to convert the variable from source to destination type.
Logically this would not be a problem. I could just do :

FsmPath doo = OutputPath.GetType() == typeof(FsmPath) ? OutputPath : (FsmPath)OutputPath;

In my thoughts this would set doo to the FsmPath variable if it was already of that type, and it would cast it to that type even if it wasn’t. Sadly the compiler still disagrees.

What can I do to solve this? How can I make it obvious to the compiler that the object variable is never something other than a FsmPath variable, without actually making the input one?

“(For certain environmental reasons it really cannot be anything but an object)”
Are you really sure of that? Maybe you could overload your function with a version that accepts FsmPath instead of Object.

Your points don’t make any sense to me. I don’t see any reason why you have to use a variable of type object when it’s always a FsmPath. The only reason would be because it’s not always a FsmPath. In this case the compiler doesn’t know the type at compile time, so you have to cast it to your desired type. A cast only works when the object stored in the object variable is a FsmPath. If it’s not a FsmPath you can’t turn it into a FsmPath.

This line:

FsmPath doo = OutputPath.GetType() == typeof(FsmPath) ? OutputPath : (FsmPath)OutputPath;

also makes no sense at all. You use reflection to test if the stored object is of type FsmPath and if it is you just assign the object variable to the FsmPath variable which doesn’t work without a cast. Even worse is your alternative case. If it’s not a FsmPath you try to cast it to FsmPath which is impossible since the object is not a FsmPath.

The usual way to text for a specific type is the “is” operator:

if (OutputPath is FsmPath)
{
    FsmPath fsmPath = (FsmPath)OutputPath;
    // do something with the fsmPath
}
else
{
    // It's not a FsmPath so either test for other possible types or do nothing.
}

ps: You have to distinguish between value-conversion-casts and type casting. A conversion cast will transform the source object into something different. That type of cast is mostly used for value types. (for example casting a float to an int).

Type casts however can’t convert anything. It just tells the compiler that you “think” this object should be of the given type. When it’s not the runtime will throw an exception.