Using reflection to properly cast an object

  • Thread starter Thread starter zfeld
  • Start date Start date
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.)
 
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.
 
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
 
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?
 
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.
 
Back
Top