using a switch statement to evaluate objects

Last post 05-12-2008 7:59 AM by hspharic. 2 replies.

Sort Posts:

  • using a switch statement to evaluate objects

    05-09-2008, 11:32 AM
    • Loading...
    • jonny75904
    • Joined on 04-24-2006, 3:33 PM
    • Houston TX
    • Posts 104

     Is it possible to use a switch statement to evaluate if an object inherits a particular interface?  Something like:

     

    switch(mystery content)

    {

    case IList:
    //blah
    break;

    case IDictionary:
    //blah
    break;

    }

     

  • Re: using a switch statement to evaluate objects

    05-09-2008, 11:54 AM
    • Loading...
    • gbogea
    • Joined on 04-14-2008, 11:17 PM
    • Brazil
    • Posts 204

    No.

    The types of the values a switch statement operates on can be booleans, enums, integral types, and strings

    Gabriel Bogéa (http://www.gbogea.com)
    -----------------
    Please 'Mark as Answer' the post(s) that helped you
  • Re: using a switch statement to evaluate objects

    05-12-2008, 7:59 AM
    • Loading...
    • hspharic
    • Joined on 02-27-2007, 2:18 AM
    • Erode,Tamilnadu - India
    • Posts 112

    Hi,

    Try this,

    string strTypes = string.Empty;

    hspTestClass obj = new hspTestClass();

    for(int cnt = 0; cnt < obj.GetType().GetInterfaces().Length; cnt++)

    {

    switch (obj.GetType().GetInterfaces()[cnt].Name)

    {

    case "IDisposable":

    strTypes += "IDisposable @#@";

    break;

    case "IList":

    strTypes += "IList @#@";

    break;

    case "IEnumerable":

    strTypes += "IEnumerable @#@";

    break;

    }

    }

    Hope this will help you.

    Yes Happy Coding...

    Hariharasudhan Chandiramurthy


    Click Answered when you satisfied my replies.
Page 1 of 1 (3 items)