If Page is typeof custom class
<p>I'm trying to work out whether my webforms <code>Page</code> is a certain type.</p>

<p>I've got a number of base classes that inherit one another:</p>

<pre><code>BasePage -&gt; System.Web.UI.Page
CMSPage -&gt; BasePage
Page -&gt; CMSPage
</code></pre>

<p>In a custom <code>UserControl</code>, I need to get the the page, but only if it's derived from <code>CMSPage</code>.</p>

<p>I've tried:</p>

<pre><code>if(Page.GetType() == typeof(CMSPage))
</code></pre>

<p>But the value of GetType() just returns the class name of the page. It looks like I need to do:</p>

<pre><code>if(Page.GetType().BaseType.BaseType == typeof(CMSPage))
</code></pre>

<p>To get the match working. However, some pages are at different <code>BaseType</code> levels before it reaches the type of <code>CMSPage</code>.</p>

<p>I could create a function like this to loop through each <code>BaseType</code> until it found the right one:</p>

<pre><code>public static bool IsPageTypeOf(this System.Web.UI.Page page, Type targetType)
{
    var pageType = page.GetType();
    if (pageType == targetType)
        return true;
    while (pageType.BaseType != null)
    {
        if (pageType.BaseType == targetType)
            return true;
        pageType = pageType.BaseType;
    }
    return false;
}
</code></pre>

<p>But it feels messy. Is there a better way to do this?</p>
