GetMethod of a private method on a Page doesn't work

E

ewolfman

Hi,

I'm trying to retrieve a private method using reflection, which exists
in my ASP.NET Page.
It works only if I change the method's accessor to 'protected'.

BTW: this occurs only for Page's private methods; if I create a new
class within the page's file, it works fine.

protected void Page_Load(object sender, EventArgs e)
{
Type type = this.GetType();

MethodInfo[] methods = type.GetMethods(BindingFlags.Instance |
BindingFlags.NonPublic);
foreach (MethodInfo method in methods)
{
if (method.Name.Equals("AA"))
{
int a = 0; // place breakpoint here
}

Debug.WriteLine(method.Name);
}
}

protected void AA() // change this to 'private' and rerun....
{
}
}

Please assist.
Thanks.
 
K

Karl Seguin

you are getting the wrong type.

public class Index : Page
{
....
this.GetType()
}


this.GetType() will be Index_aspx because the actual type of this is the
aspx (not .cs) file being JITed which only inherits from Index.

to fix, you do:

Type type = typeof(Index);

voila.

It's a pretty easy problem to debug, just add a watch to the type variable.

Karl
 
E

ewolfman

Hi Karl,

You're right, of course. I should have guessed it (and I promise you I
have looked at the type's watch for a long time, but I was wrong in my
understanding of the ASP.NET Page inheritance module).

However, I'm still puzzled - why this works with an accessor of
protected (or public). Shouldn't this behaviour be consistent ? why the
difference ?
 
K

Karl Seguin

You know, when i answered this 5 days ago, that was clear to me. This
morning it isn't :)

My guess is that it's visible to the ASPX page because it inherits protected
and public members. Much like the ToString() is visible from all objects
which inherit from Object. However, the ASPX page does not inherit private
members, thus it isn't visible at that scope.

Karl
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top