views:

538

answers:

3

I want to implement Generics in my Page Class like :

Public Class MyClass(Of TheClass)
Inherits System.Web.UI.Page

But for this to work, I need to be able to instantiate the Class (with the correct Generic Class Type) and load the page, instead of a regular Response.Redirect. Is there a way to do this ?

A: 

I'm no ASP.NET guru, but I don't see how. How do you intend on using the class?

Ty
+2  A: 

I'm not sure to fully understand what you want to do. If you want something like a generic Page, you can use a generic BasePage and put your generic methods into that BasePage:

Partial Public Class MyPage
    Inherits MyGenericBasePage(Of MyType)

End Class

Public Class MyGenericBasePage(Of T As New)
    Inherits System.Web.UI.Page

    Public Function MyGenericMethod() As T
        Return New T()
    End Function

End Class

Public Class MyType

End Class
Costo
A: 

The answer that says to derive a type from the generic type is a good one. However, if your solution involves grabbing a page based upon a type determined at runtime then you should be able to handle the PreRequestHandlerExecute event on the current HttpApplication.

This event is called just before a Request is forwarded to a Handler, so I believe you can inject your page into the HttpContext.Current.Handler property. Then you can create the page however you wish.

Gilligan