Groups > Asp .Net > Advanced ASP.NET Architecture > Re: Reflection: Invoke a private method with arguments




Reflection: Invoke a private method with arguments

Reflection: Invoke a private method with arguments
Thu, 3 Apr 2008 21:12:01 +0000
Dear friends,

 How I invoke a private method with parameters?
 This is the declaration of the method:
private void ShowGrid(string pSortExpression)

 This is the code that I'm using:
Type t = obj.GetType();
MethodInfo m = t.GetMethod("ShowGrid", BindingFlags.Public |
BindingFlags.NonPublic, null, new Type[] { typeof(System.String)}, null);
if (m != null) 
     m.Invoke(Page, new Object[] { "column_name" });

Obs: obj is a valid object

 If I change the method ShowGrid to public the invoke works.
Ex:
Declaration:
public void ShowGrid(string pSortExpression)

Code:
Type t = obj.GetType();
MethodInfo m = t.GetMethod("ShowGrid", new Type[] {
typeof(System.String) });
if (m != null) 
     m.Invoke(Page, new Object[] { "column_name" });

It works 100%.
The problem is if I change the method to private.
Post Reply
Re: Reflection: Invoke a private method with arguments
Thu, 3 Apr 2008 21:33:01 +0000
Hi rasl,

If I'm understanding you right you want to reflect the private function ShowGrid
with parameters.  It looks like you were reflecting:

MethodInfo m = t.GetMethod("ResetControls", BindingFlags.Public |
BindingFlags.NonPublic, null, new Type[] { typeof(System.String)}, null);

ResetControls, not ShowGrid, in your first example.

 Pete
Post Reply
Re: Reflection: Invoke a private method with arguments
Thu, 3 Apr 2008 21:34:10 +0000
You need to use 

 

BindingFlags.NonPublic | BindingFlags.Instance
Post Reply
Re: Reflection: Invoke a private method with arguments
Thu, 3 Apr 2008 21:41:29 +0000
Yes, i'm wrote wrong in the text.

The method i want to invoke is ShowGrid OK.

I'll edit my question.
Post Reply
Re: Reflection: Invoke a private method with arguments
Thu, 3 Apr 2008 23:12:04 +0000
I useBindingFlags.NonPublic | BindingFlags.Instance, but not works.

I change the method to protected and works, but private not.
Post Reply
about | contact