how to test if an object supports a given interface at runtime

Discussion in 'ASP .Net' started by MrB, Jan 26, 2004.

  1. MrB

    MrB Guest

    Hi,
    I'm writing an asp.net app in vb.net and many of my ascx classes support an
    interface IAutoSave, but some do not.
    I want to test in my code if the class behind a given ascx control supports
    this interface. Currently I'm trapping for an error on converting the class
    to this interface but am wondering if there is a more direct way of testing
    if an object supports a given interface.
    How do I do this?
    Thanks in advance,
    Jim
     
    MrB, Jan 26, 2004
    #1
    1. Advertisements

  2. MrB

    Jerry III Guest

    I think you should take a look at System.Type.IsSubclassOf

    Jerry

    "MrB" <> wrote in message
    news:...
    > Hi,
    > I'm writing an asp.net app in vb.net and many of my ascx classes support

    an
    > interface IAutoSave, but some do not.
    > I want to test in my code if the class behind a given ascx control

    supports
    > this interface. Currently I'm trapping for an error on converting the

    class
    > to this interface but am wondering if there is a more direct way of

    testing
    > if an object supports a given interface.
    > How do I do this?
    > Thanks in advance,
    > Jim
    >
    >
     
    Jerry III, Jan 26, 2004
    #2
    1. Advertisements

  3. You can do the following:

    IAutoSave autoSave = someObject as IAutoSave;
    if(autoSave != null)
    {
    ...
    }

    HTH

    nick robinson
    site : www.fromconcept.co.uk
    blog : www.fromconcept.co.uk/weblog.aspx

    ----- MrB wrote: -----

    Hi,
    I'm writing an asp.net app in vb.net and many of my ascx classes support an
    interface IAutoSave, but some do not.
    I want to test in my code if the class behind a given ascx control supports
    this interface. Currently I'm trapping for an error on converting the class
    to this interface but am wondering if there is a more direct way of testing
    if an object supports a given interface.
    How do I do this?
    Thanks in advance,
    Jim
     
    =?Utf-8?B?Tmljaw==?=, Jan 26, 2004
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. joes
    Replies:
    2
    Views:
    463
    Roland
    Feb 21, 2005
  2. cyberco
    Replies:
    8
    Views:
    665
    cyberco
    Feb 25, 2006
  3. Lord0
    Replies:
    1
    Views:
    774
    Thomas Weidenfeller
    Apr 19, 2006
  4. chiara
    Replies:
    6
    Views:
    742
    Barry Schwarz
    Oct 6, 2005
  5. Ami
    Replies:
    3
    Views:
    539
    Tomás
    Feb 27, 2006
  6. 2Barter.net
    Replies:
    0
    Views:
    604
    2Barter.net
    Dec 13, 2006
  7. Steve Kershaw
    Replies:
    1
    Views:
    469
    Teemu Keiski
    May 24, 2007
  8. Casey Hawthorne
    Replies:
    385
    Views:
    8,068
    ng2010
    Apr 4, 2010
Loading...