Last post Jul 27, 2010 07:09 PM by brad.heller
Dec 04, 2009 05:09 AM|bgs264|LINK
I've got a web user control (.ascx) which implements a couple of interfaces I wrote; namely IXMLBoundControl and ISectionOverridingControl.
I've written a mini-CMS type application that dynamically loads controls onto the page based on information in a database. When I click a button on a web page (.aspx), I want to look at all the controls on that page, and determine if there is a control which
implements the ISectionOverridingControl.
I've got my loop and I'm looping through the controls; that's fine. However I'm not sure what the best way is to determine whether or not the control implements the interface. What I'm doing at the moment (and works) is to try to cast each control into the
ISectionOverridingControl and catching InvalidCastException: If I don't get the catch; I consider it's worked. If the exception is thrown then it doesn't implement the interface.
It's working, however, I consider this inefficient (relying on exceptions): surely there's a better way in VB.NET (I've seen an example in C# but it didn't convert to VB) to tell whether an instance of a class implements an interface or not?
Appreciate any/all help.
Dec 04, 2009 06:02 AM|anas|LINK
I want to look at all the controls on that page, and determine if there is a control which implements the ISectionOverridingControl.
If TypeOf(yourcontrol) Is ISectionOverridingControl
' the control implmenets ISectionOverridingControl
Dec 04, 2009 06:56 AM|bgs264|LINK
Jul 27, 2010 07:09 PM|brad.heller|LINK
Best solution that I've come up with is as follows:
public static class TypeExtensions
public static bool IsImplementationOf(this Type baseType, Type interfaceType)