ASP.NET Reflection problem

G

Guest

Hello,

I have an asp.net web page (say page.aspx) which derives from a custom base
page object (CustomPage : BasePage : System.Web.UI.Page) Which has a method
called DoSomething(params). My web page contains a UserControl (say
Control.ascx).

In the control class, i want to call the DoSomething method. I cannot use
type cast since in fact the base class Page structure is a little more
complex (see bottom of post). Hence i cannot do:
CType(Me.Page, BasePage).DoSomething(params)

instead i use reflection

Me.Page.GetType().InvokeMember("DoSomething", BindingFlags.InvokeMethod,_
Nothing, Me.Page, New Object() {param})
'This is pretty much the same type of code used to call methods from a
master
'page in a UserControl (since master page's data type in not accessible
from a
'usercontrol)

In any .NET App or Web app, this works well. But for some reason, it doesn't
work in a UserControl. (This code works fine in the aspx code behind but not
in the ascx code behind).

In a UserControl's code behind, this throws a MissingMethodException

Anyone already experienced this issue?

What is very strange here is that the following code does work

Dim mi As Reflection.MethodInfo = Me.Page.GetType().GetMethod("DoSomething")
mi.Invoke(Me.Page, Reflection.BindingFlags.InvokeMethod, Nothing, New
Object() {param}, Nothing)

Thanks

Renaud

==============================
Base Class Page structure

Class FoundationBasePage : System.Web.UI.Page
Class FoundationBaseFirstPage : FoundationBasePage
Class FoundationBaseLastPage : FoundationBasePage

Class BasePage : FoundationBasePage
Class BaseFirstPage : FoundationBaseFirstPage
Class BaseLastPage : FoundationBaseLastPage

The method DoSomething is located in BasePage, BaseFirstPage and BaseLastPage
The UserControl may be used in pages inheriting from any of those three base
classes.
 
B

bruce barker

the methods needs to be public. a better approach is for the page to
implement an interface, then the user control can cast the Page to the
interface, and call the method directly, rather than use reflection.

-- bruce (sqlwork.com)
 
G

Guest

Thanks for your reply. I feel a bit stupid that i didn't think about the
interface...

Yet, it should work. The function i am calling is public (it works when
called from the aspx code behind).

I already solved the problem using the GetMethod call (exposed later in my
previous post) but i think designing an interface is more neat and is
probably faster.

Thanks a lot

Renaud
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top