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

Discussion in 'ASP .Net' started by ewolfman, Nov 10, 2005.

  1. ewolfman

    ewolfman Guest

    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.
    ewolfman, Nov 10, 2005
    #1
    1. Advertising

  2. ewolfman

    Karl Seguin Guest

    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

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/
    http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



    "ewolfman" <> wrote in message
    news:...
    > 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.
    >
    Karl Seguin, Nov 10, 2005
    #2
    1. Advertising

  3. ewolfman

    ewolfman Guest

    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 ?
    ewolfman, Nov 11, 2005
    #3
  4. ewolfman

    Karl Seguin Guest

    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

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/

    "ewolfman" <> wrote in message
    news:...
    > 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 ?
    >
    Karl Seguin, Nov 15, 2005
    #4
  5. ewolfman

    ewolfman Guest

    Hmmm.....

    Makes sense....

    Thanks for your patience!
    ewolfman, Nov 15, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    12,183
  2. Joe Weinstein
    Replies:
    2
    Views:
    510
    Chris Uppal
    Mar 18, 2006
  3. fAnSKyer

    getmethod in reflection

    fAnSKyer, Oct 24, 2006, in forum: Java
    Replies:
    4
    Views:
    473
    Mike Schilling
    Oct 25, 2006
  4. chucky
    Replies:
    14
    Views:
    1,267
    Twisted
    Aug 2, 2007
  5. Alexander Burger

    getMethod() works and works not

    Alexander Burger, Nov 27, 2010, in forum: Java
    Replies:
    25
    Views:
    1,711
    Alexander Burger
    Nov 29, 2010
Loading...

Share This Page