dynamic casting (type known at runtime) 
Author Message
 dynamic casting (type known at runtime)

static casting is simple:

A a = (A)b;

but how to do a dynamic casting?

void Q( Type t )
{
    A a = ___ how to cast b to type t? I need someting like

    a = (t)b; // this of course won't work

    but I cannot find anything with proper functionality.

Quote:
}

Thanks for any help,
Wiktor Zychla


Tue, 03 May 2005 19:06:39 GMT  
 dynamic casting (type known at runtime)

Quote:

> static casting is simple:

> A a = (A)b;

> but how to do a dynamic casting?

> void Q( Type t )
> {
>     A a = ___ how to cast b to type t? I need someting like

>     a = (t)b; // this of course won't work

>     but I cannot find anything with proper functionality.
> }

Could I ask what you hope to then *do* with it? The main point of
casting is so that you can call appropriate methods on the cast type -
which you can't do unless you know the type beforehand anyway.

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Tue, 03 May 2005 19:26:33 GMT  
 dynamic casting (type known at runtime)

Quote:
> which you can't do

if sereval types implement the same interface, you can.

Of course, I can redesign the code if it is not possible to do such casting.



Tue, 03 May 2005 19:58:23 GMT  
 dynamic casting (type known at runtime)
The solution would be to statically cast to the interface:

public interface IFeedable {
    public void Feed() {
    }

Quote:
}

public class Dog : IFeedable {
    ...

Quote:
}

public class Cat : IFeedable {
    ....

Quote:
}

public void someMethod() {
    object [] objectArray = { new Dog("Ralph"), new Dog("Spot"), new
Cat("Felix") };

    foreach (object theObj in objectArray) {
        (IFeedable)theObj.Feed();
    }

Quote:
}

public

Quote:
> > which you can't do

> if sereval types implement the same interface, you can.

> Of course, I can redesign the code if it is not possible to do such
casting.



Tue, 03 May 2005 20:27:32 GMT  
 dynamic casting (type known at runtime)

Quote:
> The solution would be to statically cast to the interface:

this is it.
I overlooked that.

thanks



Tue, 03 May 2005 20:56:55 GMT  
 dynamic casting (type known at runtime)

Quote:

> > which you can't do

> if sereval types implement the same interface, you can.

In that case, you should cast to that interface type - simple!

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Tue, 03 May 2005 21:39:33 GMT  
 dynamic casting (type known at runtime)

Quote:


>> static casting is simple:

>> A a = (A)b;

>> but how to do a dynamic casting?

>> void Q( Type t )
>> {
>>     A a = ___ how to cast b to type t? I need someting like

>>     a = (t)b; // this of course won't work

>>     but I cannot find anything with proper functionality.
>> }

>Could I ask what you hope to then *do* with it? The main point of
>casting is so that you can call appropriate methods on the cast type -
>which you can't do unless you know the type beforehand anyway.

You might know the method, or be searching to see if a type implements
a method, and then call it using the reflection classes, so I can see
where this might be useful.

Jonathan Schafer



Tue, 03 May 2005 22:59:46 GMT  
 dynamic casting (type known at runtime)

Quote:

> >Could I ask what you hope to then *do* with it? The main point of
> >casting is so that you can call appropriate methods on the cast type -
> >which you can't do unless you know the type beforehand anyway.

> You might know the method, or be searching to see if a type implements
> a method, and then call it using the reflection classes, so I can see
> where this might be useful.

But if you use reflection then casting like this is unnecessary!

^Tum



Thu, 12 May 2005 21:49:29 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Dynamic Type Casting at Runtime

2. Dynamic casting at runtime

3. Cast Type of Class at Runtime?

4. Runtime type-casting

5. Cast to a dynamic type?

6. dynamic type casting

7. Storing a type for dynamic casts...

8. Type Casting between Base and Derived Type (Serialization of objects with Type members)

9. type casting to / from pointers to incomplete data types

10. type casting to / from pointers to incomplete data types

11. cast cstring type to integer type

12. Casting to/from STL types to MFC types

 

 
Powered by phpBB® Forum Software