By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Using reflection to properly cast an object

P: n/a
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.)
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
zfeld <zf******@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
zfeld wrote:
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
Nov 16 '05 #3

P: n/a
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?

Nov 16 '05 #4

P: n/a
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.

"aa7im" <jo**@nautilusnet.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

Nov 16 '05 #5

P: 2
I am facing The same Situation I also have to cast at runtime.
If any Body have sloved it then Please Help.

protected void Page_Load(object sender, EventArgs e)
{
Circle circleObj = new Circle();
circleObj.Radius = 5;
Response.Write(chkRefrence(circleObj)+"<br \\>");
}
private string chkRefrence(Shape shapeObject)
{
Type t = shapeObject.GetType();
//Response.Write(t.Name.ToString());//Here I am getting Circle
((t)shapeObject).Radius = 15;//I have to Cast here
shapeObject.Id = "TestObj ";
return shapeObject.ToString();
}
May 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.