Pre Cast

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?
 
M

Marina

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.
 
J

John A. Bailo

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.
 
J

John A. Bailo

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.
 
M

Marina

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:

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top