473,857 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ASP.NET Reflection problem

Hello,

I have an asp.net web page (say page.aspx) which derives from a custom base
page object (CustomPage : BasePage : System.Web.UI.P age) Which has a method
called DoSomething(par ams). 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).DoSom ething(params)

instead i use reflection

Me.Page.GetType ().InvokeMember ("DoSomethin g", BindingFlags.In vokeMethod,_
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 MissingMethodEx ception

Anyone already experienced this issue?

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

Dim mi As Reflection.Meth odInfo = Me.Page.GetType ().GetMethod("D oSomething")
mi.Invoke(Me.Pa ge, Reflection.Bind ingFlags.Invoke Method, Nothing, New
Object() {param}, Nothing)

Thanks

Renaud

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

Class FoundationBaseP age : System.Web.UI.P age
Class FoundationBaseF irstPage : FoundationBaseP age
Class FoundationBaseL astPage : FoundationBaseP age

Class BasePage : FoundationBaseP age
Class BaseFirstPage : FoundationBaseF irstPage
Class BaseLastPage : FoundationBaseL astPage

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.
Feb 12 '07 #1
2 2983
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)

Renaud Langis wrote:
Hello,

I have an asp.net web page (say page.aspx) which derives from a custom base
page object (CustomPage : BasePage : System.Web.UI.P age) Which has a method
called DoSomething(par ams). 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).DoSom ething(params)

instead i use reflection

Me.Page.GetType ().InvokeMember ("DoSomethin g", BindingFlags.In vokeMethod,_
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 MissingMethodEx ception

Anyone already experienced this issue?

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

Dim mi As Reflection.Meth odInfo = Me.Page.GetType ().GetMethod("D oSomething")
mi.Invoke(Me.Pa ge, Reflection.Bind ingFlags.Invoke Method, Nothing, New
Object() {param}, Nothing)

Thanks

Renaud

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

Class FoundationBaseP age : System.Web.UI.P age
Class FoundationBaseF irstPage : FoundationBaseP age
Class FoundationBaseL astPage : FoundationBaseP age

Class BasePage : FoundationBaseP age
Class BaseFirstPage : FoundationBaseF irstPage
Class BaseLastPage : FoundationBaseL astPage

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.
Feb 12 '07 #2
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

"bruce barker" wrote:
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)

Renaud Langis wrote:
Hello,

I have an asp.net web page (say page.aspx) which derives from a custom base
page object (CustomPage : BasePage : System.Web.UI.P age) Which has a method
called DoSomething(par ams). 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).DoSom ething(params)

instead i use reflection

Me.Page.GetType ().InvokeMember ("DoSomethin g", BindingFlags.In vokeMethod,_
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 MissingMethodEx ception

Anyone already experienced this issue?

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

Dim mi As Reflection.Meth odInfo = Me.Page.GetType ().GetMethod("D oSomething")
mi.Invoke(Me.Pa ge, Reflection.Bind ingFlags.Invoke Method, Nothing, New
Object() {param}, Nothing)

Thanks

Renaud

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

Class FoundationBaseP age : System.Web.UI.P age
Class FoundationBaseF irstPage : FoundationBaseP age
Class FoundationBaseL astPage : FoundationBaseP age

Class BasePage : FoundationBaseP age
Class BaseFirstPage : FoundationBaseF irstPage
Class BaseLastPage : FoundationBaseL astPage

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.
Feb 12 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2225
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use reflection. The code I supplied is based on the SqlDataAdapter with reflection. The error occurs when trying to create the delegate that will be passed in to EventInfo.AddEventHandler. I get the following error: An unhandled exception of type...
10
7381
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a Windows.Forms.UserControl in a COM environment, i.e. I want to host that control in a COM host. So far, so good, I can host it, but I can not reach the parent COM object from the control (Parent property is null :( ). I have stopped the control in the...
2
864
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray that has the information, but we as developers have no way to access this information. I have come up with a solution for the problem, (as I am sure many others have) using reflection. Here is some sample code that will print out the auto...
2
1917
by: Lev | last post by:
Hi, I have some code that does reflection on an assembly I load. When I try to get the attributes on one of the methods implemented in the assembly, the MC++ version does not return anything. See code below: Object __gc* memberAttributes = info->GetCustomAttributes(__typeof(EntryPointAttribute), false);
2
2067
by: Marco | last post by:
Hi, I try to use reflection to retrieve class B members and not inherited members. I wonder why the myMember value is nothing. Any suggestions? Thanks Module Module1 Sub Main()
3
2336
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided through an external file in which case we can use reflection to create objects of that type at run time. Case 1: The methods of those types can be invoked through reflection. Case 2: The caller can publish certain events. The types (objects)...
7
2372
by: Paul Hadfield | last post by:
Hi, I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system "Custom Attributes" by type. The output below identifies that I can catch the "DefaultMemberAttribute", but I can not catch my own "DirectoryVersionAttribute". Cany anyone throw any light on this as it is driving mad Thanks in Advance, - Paul....
5
4305
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create delegate: public class DelegateEx {
6
1973
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept quiet and smiled. What is reflection!? I have been reading it up, and all I can work out is that it has something to do with Late Binding, and the use of GetType and Type.
0
9923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9768
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11083
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10711
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10808
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7940
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7104
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5971
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4191
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.