Pre Cast

  • Thread starter Thread starter John A. Bailo
  • Start date Start date
J

John A. Bailo

I call a web service using a dynamic assembly.

Because of this, I have no way of knowing in advance, what the return
object type of the service is.

It may be an integer, it may be a string.

The way the method that invokes the service is written, it returns an
object, and then I have to cast that object as a datatype to use the value.

So, is there a way to read an object, and, based on what the value is,
cast it to one datatype or another?
 
You can of course just use typeof and check for different types.

I would suggestion looking into the GetTypeCode method of the Type class. It
takes an object, and returns an enumeration. This enumeration has all the
basic value types. You can then have a case statement where you can cast to
the appropriate type.
 
GetTypeCode doesn't seem to be a member of the Type class.

It looks like its a member of string.

So, I guess I would cast my object as string, then use GetTypeCode to
get the enum, and then cast accordingly (using a case, as you suggest).

Thanks for the input.
You can of course just use typeof and check for different types.

I would suggestion looking into the GetTypeCode method of the Type class. It
takes an object, and returns an enumeration. This enumeration has all the
basic value types. You can then have a case statement where you can cast to
the appropriate type.
 
Ok, maybe this is what you mean:

switch (Type.GetTypeCode(type))
{
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:

}

GetTypeCode doesn't seem to be a member of the Type class.

It looks like its a member of string.

So, I guess I would cast my object as string, then use GetTypeCode to
get the enum, and then cast accordingly (using a case, as you suggest).

Thanks for the input.
 
yes, that is exactly what i was talking about

John A. Bailo said:
Ok, maybe this is what you mean:

switch (Type.GetTypeCode(type))
{
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:

}
 
Back
Top