Switch on Interface Type

  • Thread starter Thread starter Smithers
  • Start date Start date
S

Smithers

How can I implement the following pseudocode in a switch block? Notice it
would need to switch on the type of interface implemented in currentObject.


if (currentObject is ISomeInterface01)
{
// do something
}

if (currentObject is ISomeInterface02)
{
// do something else
}

if (currentObject is ISomeInterface03)
{
// do some other thing
}

Otherwise, throw a new exception

Thanks!
 
Smithers said:
How can I implement the following pseudocode in a switch block? Notice it
would need to switch on the type of interface implemented in currentObject.

You can't, without resorting to hacks like finding all the interfaces
implemented and switching on their names as strings.

Why not just use the "if/else" solution? Bear in mind that it's quite
possible to implement *all* of the interfaces - you should consider
what your code should do in that situation.
 
Yes, I thought about the "switch on interface name as string" hack but
decided against it (because it's a hack)... thus the OP here. Thanks for the
other considerations... I'll be going with the if/else blocks.

-S
 

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

Back
Top