Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
there is a method in a class. I wanna acccess that private method from another class . To do that I need to create a public method inthat class whcih has getter and a setter for that private method. So that I can access that private method.how would that public method look like? can I have the syntax please. Following is my private method.


C#
private void TryLoadTransitionInfo()

public --return type------? AccessPrivateMethod
{

         get { return TryLoadTransitionInfo()};
}

is this almost right? can i have the right one please/
Posted
Updated 13-Oct-11 5:38am
v2

If you have access to the source code for that class you can change the modifier from from private to public.

C#
public void TryLoadTransitionInfo()
{
    // the code as is
}


If you do not have access to the source you will have to use reflection as follows :
C#
// obj is the object which has the TryLoadTransitionInfo method in it
MethodInfo dynMethod = obj.GetType().GetMethod("TryLoadTransitionInfo", BindingFlags.NonPublic | BindingFlags.Instance);
dynMethod.Invoke(obj, new object[] { });
 
Share this answer
 
Comments
Espen Harlinn 13-Oct-11 17:32pm    
My 5!
Mehdi Gholam 14-Oct-11 1:23am    
Cheers
You can use a getter as you have done:

C#
public --return type------? AccessPrivateMethod
{

            get { return TryLoadTransitionInfo();}
}


Look at where the semi colon has moved too!!!
 
Share this answer
 
I would like to say that I think the strategy you are pursuing here is not good OO design. If you want to publish access to a method in a Class, why not, as others commented, make it public in the first place ?

If it's the case that you don't have access to the source code in which the method is defined, then I think Mehdi's solution via reflection is the only way you can proceed.

But, in this case, I do believe you have access to the source based on your example.

If you want to return a method, in .NET 3.5 or later, you can use Action<t> for a method that has no return value, or Func<t> for a method that does have return. See the MSDN docs to understand that the number of specified parameter types to Action and Func can vary (the overloads).

Here's how that might work in your case:
C#
public class RevealprivateMethod
{
    private int addTwo(int i, int j)
    {
        return (i + j);
    }

    // create a public property of a method
    // whose signature is two int parameter inputs
    // and an int return value
    public Func<int, int, int> accessAddTwo
    {
        get { return addTwo; }
    }
}
To use the method in 'RevealPrivateMethod from outside the class, for example:
RevealprivateMethod rpmInstance = new RevealprivateMethod();
int result = rpmInstance.accessAddTwo(5, 8);
 
Share this answer
 
v7

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900