Dynamic Type Casting at Runtime 
Author Message
 Dynamic Type Casting at Runtime

I am not sure if my subject is appropriate for what I am trying to do, but
here is the issue.  There is a method I am calling that returns a generic
Object, and it's underlying Type can be a few different things, one of which
is an array of Bytes, or System.Byte[].  This Object can also be a String
and a few other things.  It's specifically being returned from a method call
to GetValue() on an instance of the Microsoft.Win32.RegistryKey class.  At
compile time I don't know what it's underlying type will actually be.  It
all depends on what key RegistryKey is working with and what values it
contains.

Is there a way to cast the Object to its appropriate underlying type without
writing an if statement for each possible type it could be?  I can grab the
Type at runtime by using GetType() on the Object, which is fine.  For
instance:

---

object data = registryKey.GetValue();
Type type = data.GetType();

// If I knew data was a Byte[] array, by using the Type information
byte[] dataCastedKnown = (byte[]) data;

// If I didn't know at compile time, throws an InvalidCastException
if (type.IsArray)
    object[] dataCastedUnknown = (object[]) data;

or

type.ActualRuntimeDataType? dataCastedUnknown =
(type.ActualRuntimeDataType?) data;
--

This may be a little far fetched...maybe I really have to write if else
statements for each possible case that may come back, any suggestions?

Jason



Sat, 19 Jul 2003 02:49:35 GMT  
 Dynamic Type Casting at Runtime

Quote:
>Is there a way to cast the Object to its appropriate underlying type without
>writing an if statement for each possible type it could be?  

Even if you could do

ob.MagicalConvertMeToMyActualType()

...what would you store it in?



Sat, 19 Jul 2003 03:05:37 GMT  
 Dynamic Type Casting at Runtime
I knew that sounded like a stretch...what about this.  Why does casting an
object which is actually a byte[] to an object[] throw an
InvalidCastException?

object data = registryKey.GetValue();
Type type = data.GetType();

if (type.IsArray)
{
    object[] dataArray = (object[]) data;

    foreach (objectData in dataArray)
   {
      // process here
   }

Quote:
}



Quote:
> >Is there a way to cast the Object to its appropriate underlying type
without
> >writing an if statement for each possible type it could be?

> Even if you could do

> ob.MagicalConvertMeToMyActualType()

> ...what would you store it in?



Sat, 19 Jul 2003 03:16:37 GMT  
 Dynamic Type Casting at Runtime

Quote:
>I knew that sounded like a stretch...what about this.  

Not really, I just thought about one case where you might want to do
this. Say you have a class with overloaded constructors for different
types, then you might want to do something like this (forgive syntax)

x mX = new x((IConvertible ob).ToType(ob.GetType()));

Quote:
>Why does casting an
>object which is actually a byte[] to an object[] throw an
>InvalidCastException?

Because arrays do not support bulk conversion I guess.


Sat, 19 Jul 2003 03:48:44 GMT  
 Dynamic Type Casting at Runtime
try the "as" statement. This way you only need to check if you get null or
not.

Jason


Quote:
> I am not sure if my subject is appropriate for what I am trying to do, but
> here is the issue.  There is a method I am calling that returns a generic
> Object, and it's underlying Type can be a few different things, one of
which
> is an array of Bytes, or System.Byte[].  This Object can also be a String
> and a few other things.  It's specifically being returned from a method
call
> to GetValue() on an instance of the Microsoft.Win32.RegistryKey class.  At
> compile time I don't know what it's underlying type will actually be.  It
> all depends on what key RegistryKey is working with and what values it
> contains.

> Is there a way to cast the Object to its appropriate underlying type
without
> writing an if statement for each possible type it could be?  I can grab
the
> Type at runtime by using GetType() on the Object, which is fine.  For
> instance:

> ---

> object data = registryKey.GetValue();
> Type type = data.GetType();

> // If I knew data was a Byte[] array, by using the Type information
> byte[] dataCastedKnown = (byte[]) data;

> // If I didn't know at compile time, throws an InvalidCastException
> if (type.IsArray)
>     object[] dataCastedUnknown = (object[]) data;

> or

> type.ActualRuntimeDataType? dataCastedUnknown =
> (type.ActualRuntimeDataType?) data;
> --

> This may be a little far fetched...maybe I really have to write if else
> statements for each possible case that may come back, any suggestions?

> Jason



Sat, 19 Jul 2003 06:41:41 GMT  
 Dynamic Type Casting at Runtime
There's no way around checking for each type. You could use the "is" or "as"
operators to do this.

I'd avoid doing the cast and catching exceptions, as this may not perform as
well.


Quote:
> I am not sure if my subject is appropriate for what I am trying to do, but
> here is the issue.  There is a method I am calling that returns a generic
> Object, and it's underlying Type can be a few different things, one of
which
> is an array of Bytes, or System.Byte[].  This Object can also be a String
> and a few other things.  It's specifically being returned from a method
call
> to GetValue() on an instance of the Microsoft.Win32.RegistryKey class.  At
> compile time I don't know what it's underlying type will actually be.  It
> all depends on what key RegistryKey is working with and what values it
> contains.

> Is there a way to cast the Object to its appropriate underlying type
without
> writing an if statement for each possible type it could be?  I can grab
the
> Type at runtime by using GetType() on the Object, which is fine.  For
> instance:

> ---

> object data = registryKey.GetValue();
> Type type = data.GetType();

> // If I knew data was a Byte[] array, by using the Type information
> byte[] dataCastedKnown = (byte[]) data;

> // If I didn't know at compile time, throws an InvalidCastException
> if (type.IsArray)
>     object[] dataCastedUnknown = (object[]) data;

> or

> type.ActualRuntimeDataType? dataCastedUnknown =
> (type.ActualRuntimeDataType?) data;
> --

> This may be a little far fetched...maybe I really have to write if else
> statements for each possible case that may come back, any suggestions?

> Jason



Sat, 19 Jul 2003 06:58:03 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. dynamic casting (type known 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