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

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

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

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:
    356
    Roland
    Feb 21, 2005
  2. cyberco
    Replies:
    8
    Views:
    478
    cyberco
    Feb 25, 2006
  3. Lord0
    Replies:
    1
    Views:
    548
    Thomas Weidenfeller
    Apr 19, 2006
  4. chiara
    Replies:
    6
    Views:
    456
    Barry Schwarz
    Oct 6, 2005
  5. Ami
    Replies:
    3
    Views:
    406
    Tomás
    Feb 27, 2006
Loading...

Share This Page