Click to See Complete Forum and Search --> : Cast at runtime
Marrigon
May 7th, 2003, 08:14 AM
Hi,
is there a way to define a cast at runtime, instead of hardcoding it (i.e. using Reflection)?
Let me explain the problem:
I've to call a method which returns an object
public abstract object GetInfo()
Each class that derives from this base class must define the method, but obviously it can return any type of object.
Using reflection I can know the Type of the returned class, but how can I cast it in it's real type (for example because I need to assign it to a variable)?
string myText;
myText = (???) myClass.GetInfo();
In this case GetInfo() return a string.
Thanks
Marrigon
KingTermite
May 7th, 2003, 08:51 AM
Can you use a switch statement?
Psuedocode example:
switch(obj)
{
case (obj1): myText = (obj1) myClass.GetInfo();
case (obj2): myText = (obj2) myClass.GetInfo();
case (obj3): myText = (obj3) myClass.GetInfo();
}
Another thought....if all the object have this same method, returning same type...why do you need to cast at all ?
Marrigon
May 7th, 2003, 09:01 AM
Unfortunately I can't because I'm not the only one who will extend the base class (and therfore a new implementation of GetInfo() ). It's not possible to udpate the switch block each time a new type is returned.
KingTermite
May 7th, 2003, 09:41 AM
Originally posted by Marrigon
Unfortunately I can't because I'm not the only one who will extend the base class (and therfore a new implementation of GetInfo() ). It's not possible to udpate the switch block each time a new type is returned.
Aaaah. I see...you don't necessarily know what type it may be "at all".
I'm not sure the....sounds like somethin you "should" be able to do...but I'm not sure. I understand your problem now. You need to get the "type" of that object "on the fly".
Gurus?
Marrigon
May 7th, 2003, 12:27 PM
I found out how to do it.
Since the returned object form GetInfo() must be used to set a property of another class, is possible to use Reflection and in paticular SetValue.
public abstract class MyClassA
{
public abstract object GetInfo();
}
public class MyClassB
{
public void SetPropertyValue(string pPropertyName, object pValue)
{
// Use reflection to get the list of parameters
MemberInfo[] members = this.GetType().FindMembers(MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance,
new System.Reflection.MemberFilter(Filter), null);
foreach (PropertyInfo prop in members)
{
if (prop.Name == pPropertyName)
{
prop.SetValue(this, pValue, null);
break;
}
}
}
}
then
MyClassA a = new MyClassA();
MyClassB b = new MyClassB();
b.SetPropertyValue("propertyName", a.GetInfo());
I hope this could be useful to someone else.
Bye
Marrigon
KingTermite
May 7th, 2003, 05:20 PM
I'm sure it could. Thanks for sharing.
pareshgh
May 7th, 2003, 06:04 PM
Marrigon ,
thanks much
nyint
May 7th, 2003, 08:15 PM
Marrigon,
It's more useful to get some experience from you ..
thanks ...
nyint
wolfofthenorth
May 8th, 2003, 10:15 PM
If the derived classes override the base class implementation of a method, you should be able to cast it as the base type, and have the call actually go to the most derived impementation.
Would this work for you?
Marrigon
May 9th, 2003, 03:37 AM
No, not in my case.
I've difined an abstract class (MyClassA) which has an abstract method GetInfo(). Each class derived from MyClassA must implement GetInfo() but it can return every type of object (String, ArrayList ...).
I need to use GetInfo to set the value of a property of another class (MyClassB) which is defined as a specific object type (HashTable, String ...). Since you cannot perform an implicit cast from Object to another type you cannot write:
MyClassA a = MyClassA();
MyClassB b = MyClassB();
b.TargetProperty = a.GetInfo();
where, for example b.TargetProperty is defined as ArrayList.
I need this because in my application I've got a lot of "MyClassB" with different properties and several "MyClassA" each with its own implementation of GetInfo(). Using a custom attribute I can know the type of object return by GetInfo() (using reflection) and then (again with reflection) I can check which "MyClassA" can be used to set a specific property of "MyClassB". In this way my code don't need to be modified even if a new "MyClassA" r "MyClassB" is added.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.