Dror
Dror

Reputation: 177

How can I cast an object to the original type?

I have a method that can return many types of objects (which is why it returns an instance of type object).

How can I cast it to its original type? (The question is about casting to its original type, when I don't know its type at run time. It is not about casting when I know its type; that's why GetAnyObject method is black box)

private void func()
{
    var obj = GetAnyObject();
    callFunc(obj);
}
private void callFunc(object o)
{
    var a = o.GetType();
    ...
}

Upvotes: 0

Views: 2700

Answers (3)

Kędrzu
Kędrzu

Reputation: 2425

Casting objects to types have sense only in compile time. How you would react to the casted object in your code?

The object already has it's "original" type. The case is, when you threat it as an object you don't know its type in compile time. In runtime of course it will act as it is the "original" type you call.

Suppose you have a class, that overrides ToString() method:

public class Foo 
{
    public override string ToString() { return "I am foo"; }
}

Then you have a piece of code:

Foo foo = new Foo();
object obj = foo;
// obj is of course Foo type
Console.Write(obj.ToString());

It will of course result in writing "I am foo" to the console, because the variable obj is of type Foo, and it will act properly.

Then suppose we have a class:

public class Bar 
{
    public override string ToString() { return "I am bar"; }

    public void Foobar() {}
}

And piece of not real code that you wish to have:

object obj = new Bar();

// your runtime type casting here:
var casted = (obj.GetType()) obj;

Question is: what will you do with your casted variable? Can you call method Foobar()? How will you know if you can call method Foobar() in compile time, when you don't know the type?

Maybe the thing you seek is polymorphism? Maybe reflection? Maybe dynamic objects?

EDIT: From what you say, you need a reflection, a mechanism for investigating types in runtime. See this:

var type = obj.GetType();
var props = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach(var prop in props)
{
    Console.WriteLine("property: {0} type:{1}", prop.Name, prop.PropertyType);
}

Upvotes: 0

Kapol
Kapol

Reputation: 6463

If the possible types have something in common, you might want to create an interface which all these types implement and cast the result to that interface type.

private void func()
{
    var obj = (IMyInterface)GetAnyObject();
    callFunc(obj);
}

private void callFunc(IMyInterface o)
{
    ...
}

In one of your comments you say that the object is an array. Maybe IEnumerable will suffice, then?

EDIT

You stated that you are using third-party code which won't be changed. Hence all the objects that you get must have something in common to make your code at least a bit generic. Otherwise you'll end up with a lot of ifs for different scenarios. One things that comes to my mind is retrieving a MemberInfo object through reflection and invoking it. That assumes all types have that member defined. I am talking about something similar to the following:

MethodInfo someMethod = obj.GetType().GetMethod("M");
object result = someMethod.Invoke(obj);

It's really hard to present a good solution without knowing the exact details of your predicament ;-)

Upvotes: 1

Roland Mai
Roland Mai

Reputation: 31097

You can just go ahead and cast it with as, since it will return null if the cast fails.

private void callFunc(object o)
{
    var test = a as MyClass;
    if (test != null)
    {
       // your logic
    }
}

If you have a list of classes that you need to cast to, test each of them in the body of the function above.

Upvotes: 1

Related Questions