Using reflection to properly cast an object

Z

zfeld

How do I cast an object to its proper class at runtime given its System.Type
I have code that looks like this:

MyObject class has subclasses of MySubObjectA & MySubObjectB:


MyObject obja = new MySubObjectA ();
MyObject objb = new MySubObjectB ();

ArrayList list = new ArrayList();

list.Add(objb );
list.Add(obja );

ArrayList MyObjBs= GetAllOfType( typeof( MySubObjectB ) );
ArrayList MyObjAs = GetAllOfType( typeof( MySubObjectA ) );

public ArrayList GetAllOfType(System.Type t){
ArrayList result = new ArrayList();
foreach(object o in list){
Type objType = o.GetType();
if(objType.Equals(t))
result.Add( (t) o); //Doesn't compile
result.Add( o as t); //Doesn't compile
}
}

How do I get the objects properly casted to its class before I push em onto
my list? (I know that they are stored in the list as objects anyway, This
code isn't real, its purpose is only to express the question.)
 
J

Jon Skeet [C# MVP]

zfeld said:
How do I cast an object to its proper class at runtime given its System.Type

You can't - a cast is a compile-time concept.
I have code that looks like this:

MyObject class has subclasses of MySubObjectA & MySubObjectB:

MyObject obja = new MySubObjectA ();
MyObject objb = new MySubObjectB ();

ArrayList list = new ArrayList();

list.Add(objb );
list.Add(obja );

ArrayList MyObjBs= GetAllOfType( typeof( MySubObjectB ) );
ArrayList MyObjAs = GetAllOfType( typeof( MySubObjectA ) );

public ArrayList GetAllOfType(System.Type t){
ArrayList result = new ArrayList();
foreach(object o in list){
Type objType = o.GetType();
if(objType.Equals(t))
result.Add( (t) o); //Doesn't compile
result.Add( o as t); //Doesn't compile
}
}

How do I get the objects properly casted to its class before I push em onto
my list? (I know that they are stored in the list as objects anyway, This
code isn't real, its purpose is only to express the question.)

Casting them would have no effect. As you say, they're all just object
references as far as the list is concerned.
 
H

Hans Kesting

zfeld said:
How do I cast an object to its proper class at runtime given its
System.Type I have code that looks like this:

MyObject class has subclasses of MySubObjectA & MySubObjectB:


MyObject obja = new MySubObjectA ();
MyObject objb = new MySubObjectB ();

ArrayList list = new ArrayList();

list.Add(objb );
list.Add(obja );

ArrayList MyObjBs= GetAllOfType( typeof( MySubObjectB ) );
ArrayList MyObjAs = GetAllOfType( typeof( MySubObjectA ) );

public ArrayList GetAllOfType(System.Type t){
ArrayList result = new ArrayList();
foreach(object o in list){
Type objType = o.GetType();
if(objType.Equals(t))
result.Add( (t) o); //Doesn't compile
result.Add( o as t); //Doesn't compile
}
}

How do I get the objects properly casted to its class before I push
em onto my list? (I know that they are stored in the list as objects
anyway, This code isn't real, its purpose is only to express the
question.)

It's not completely clear exactly what you want to do, so I may be wrong.
What exacly is the compile error you got?

The "ArrayList" holds only "object"s, so there is no use casting the object
you found to some type, before you put it in an ArrayList. Everything you
get out of an ArrayList is (first) reported as "object" anyway.

You can convert the ArrayList to a real array (MySubObjectB[]),
by using the ToArray method:
MySubObjectB[] msoba = (MySubObjectB [] )result.ToArray(typeof(MySubObjectB));

Did your check for the type work? Maybe you could try "if (o is t)"

Hans Kesting
 
A

aa7im

Try your comparision as:

if(objType.Equals(typeof(t)))
{
.........
}


And when you say "doesn't compile" does it really not compile or does
it throw an exception?
 
Z

zfeld

You are looking at the wrong line of code:

if(objType.Equals(t)) - compiles fine.

It is either of the next 2 lines that don't compile

result.Add( (t) o); //Doesn't compile
result.Add( o as t); //Doesn't compile
I want to cast my generic object into the specific class using reflection or
the provided object type information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top