Elad Lachmi
Elad Lachmi

Reputation: 10561

GetMethod returning null

I have an asp.net web page. This is the class implementing the page:

public partial class _Default : System.Web.UI.Page
    {
        private readonly string delegateName = "DynamicHandler";

        protected void Page_Load(object sender, EventArgs e)
        {
            EventInfo evClick = btnTest.GetType().GetEvent("Click");

            Type tDelegate = evClick.EventHandlerType;

            MethodInfo method = this.GetType().GetMethod("DynamicHandler", 
                BindingFlags.NonPublic | BindingFlags.Instance);

            Delegate d = Delegate.CreateDelegate(tDelegate, this, method);

            MethodInfo addHandler = evClick.GetAddMethod();
            Object[] addHandlerArgs = { d };
            addHandler.Invoke(btnTest, addHandlerArgs);


        }

        private void DynamicHandler(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }

I am trying to hook up an event handler dynamicly. For some reason method remains null and I can't figure out why. I have done this many times before and I can't figure out what I'm missing.

EDIT: I found that this.GetType() returns the type of the page ASP.default_aspx and not the actual type implementing the page. I don't really know how to get around this...

Upvotes: 4

Views: 8936

Answers (3)

AJ Richardson
AJ Richardson

Reputation: 6810

For the benefit of anyone else, GetMethod() can also return null if the arguments you passed do not match the arguments of the method you are trying to find. So, for example, if you are trying to find the method:

SomeMethod(int intParam, string stringParam)

You need to pass the parameter types to GetMethod:

type.GetMethod("SomeMethod", new[] { typeof(int), typeof(string) });

Or, if you want to specify special binding flags:

type.GetMethod("SomeMethod", BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(int), typeof(string) }, null);

Upvotes: 7

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61912

There are two things to be aware of.

(1) When you use:

this.GetType()

or equivalently just GetType(), the type which is returned is the actual run-time type of the actual instance. Since _Default is a non-sealed class, that type might very well be a more derived (more specialized type) than _Default, i.e. some class which has _Default as its direct or indirect base class.

If what you want is always the "constant" type _Default, use the typeof keyword, so use:

typeof(_Default)

instead of GetType(). This alone would solve your problem.

(2) Even if you specify BindingFlags.NonPublic, inherited private members are not returned. With your choice of binding flags, private methods declared in the same class (the derived class) are returned, but private methods inherited from base classes are not returned. However with internal and protected members, both the ones declared in the class itself and the ones declared in the base classes are returned.

This might make some sense, since a private method is not meant to be invoked from a derived class.

Changing the access level of your DynamicHandler method from private to e.g. protected (as suggested in the other answer) would be enough to solve your problem, as inherited protected members are selected when you use BindingFlags.NonPublic.

It is still interesting that you can't get the inherited private members with BindingFlags. Related thread C#: Accessing Inherited Private Instance Members Through Reflection. It is possible to write a method that searches all base types, of course, like:

static MethodInfo GetPrivateMethod(Type type, string name)
{
    MethodInfo retVal;
    do
    {
        retVal = type.GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
        if (retVal != (object)null)
            break;
        type = type.BaseType;
    } while (type != (object)null);
    return retVal;
}

That alone would also have solved your problem.

Upvotes: 4

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239636

As you've discovered in your edit, the actually ASPX page gets compiled into a class that inherits from _Default (where your code is located). So you need to make DynamicHandler (at least) protected rather than private.

Specify BindingFlags.FlattenHierarchy also:

this.GetType().GetMethod("DynamicHandler", 
            BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy);

Upvotes: 4

Related Questions