Testing for a type at runtime.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have been testing for a type using code like

if(obj is MyType)
. . . do somethin

Now I want to test for MyType as a return from a metho

Type ReplyType(

return typeof(MyTYpe)

.. . .
if(obj is this.RepyType()
. . . do somethin

The problem is that the 'is' operator complains that it is expecting 'Type'. The return value for RelyType() is Type so I am not sure what it is complaining about or how to fix it. Any ideas? Thank you for your help.

As a bonus I would also like to use the 'as' operator and call a constructor given a type. But I probably can figure out how to do those functions once I have the above figured out

Thanks again

Kevi
 
Thank you for your reply

This will get me part way there.

The problem is that it is possible for obj to be either A type or B type both of which are derived from C type. So I have something like this

public class A :


public class B :



Type ReplyType(

return typeof(A)


C obj = GenerateReply()

if(this.ReplyType().IsAssignableFrom(obj.GetType())
. . . do somethin

Will this still work? I only want to 'do something' if the type returned by GenerateReply() is A

I think I have just convinced myself that this may still work. Agree

Kevin
 
I think this will work for where I need the runtime equivalent of the 'is' operator. Now what about the 'as' operator? Say I have A and B types that are both derived from C. A function returns C type which may be either A or B (for now say the C is abstract). I need an object cast to the type defined by the ReplyType() function or null if the cast fails

Kevin
 
Kevin Burton said:
I think this will work for where I need the runtime equivalent of the
'is' operator. Now what about the 'as' operator? Say I have A and B
types that are both derived from C. A function returns C type which
may be either A or B (for now say the C is abstract). I need an
object cast to the type defined by the ReplyType() function or null
if the cast fails.

Casting done by the "as" operator doesn't change the object at all - it
just gives you a reference expression of a different type.
 
I need the cast because C# methods require it. Again using my example A and B inherit from C

C obj = GenerateReply(
.. .
ProcessReply(obj); // Compiler will not allow this. A cast must be done
.. .
ProcessReply(A obj



I cannot call ProcessReply with C even though it may be an A. I have to cast it to an A and the method that I would like to use is 'as'

Kevi

----- Jon Skeet [C# MVP] wrote: ----

Kevin Burton said:
I think this will work for where I need the runtime equivalent of th
'is' operator. Now what about the 'as' operator? Say I have A and
types that are both derived from C. A function returns C type whic
may be either A or B (for now say the C is abstract). I need a
object cast to the type defined by the ReplyType() function or nul
if the cast fails

Casting done by the "as" operator doesn't change the object at all - it
just gives you a reference expression of a different type
 
Kevin Burton said:
I need the cast because C# methods require it. Again using my example
A and B inherit from C.

C obj = GenerateReply()
. . .
ProcessReply(obj); // Compiler will not allow this. A cast must be done.
. . .
ProcessReply(A obj)
{
}

I cannot call ProcessReply with C even though it may be an A. I have
to cast it to an A and the method that I would like to use is 'as'.

The reason you need to do that is for the C# compiler though - in other
words, for compile-time reasons. Could you give an example of how you
want to use it at run time instead?
 
Back
Top