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

  • Thread starter Thread starter MrB
  • Start date Start date
M

MrB

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
 
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
 
Back
Top