0

I have a private static class and I am trying to access AddtwoNum() method from SomePrivateClass to SomePublicClass in C# and It is not allowing me to do so.

class SomeClass
{
    private static class SomePrivateClass
    {
        public static void AddtwoNum(int num1, int num2)
        {
            //do some stuff here
        }          
    }
    class SomePublicClass
    {
        SomePrivateClass.AddtwoNum(); //Error: The name AddtwoNum does not exist in the current context.

        // how to call AddtwoNum() method here???
    }
} 
Rahul Nagrale
  • 93
  • 2
  • 10
  • 1
    If the code is under your control why not either make the method public, or add a public method to call the private method (preferred) - failing that, you're in reflection territory - https://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method – Darren Wainwright May 29 '17 at 12:11
  • 1
    C# is doing what it is supposed to do - preventing outside access to private members. I don't understand why you think this is a problem. – Sergey Kalinichenko May 29 '17 at 12:12
  • 1
    Possible duplicate of [How do I use reflection to invoke a private method?](https://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method) – Darren Wainwright May 29 '17 at 12:12
  • Why do you want to call a method from a private class from the outside, there´s probably a reason it´s marked `private`. – MakePeaceGreatAgain May 29 '17 at 12:18
  • @HimBromBeere: I'm just curious if I can do something like this, If yes, The how? If No, Then Why? – Rahul Nagrale May 29 '17 at 12:23
  • Sure you can via reflection. But your class is marked `private` for a reason. Bypassing this access is basically a bad idea. – MakePeaceGreatAgain May 29 '17 at 12:25
  • Wasn't it supposed to be SomePrivateClass.AddtwoNum() instead of SomeotherClass.AddtwoNum()? If you fix that and put it into a method, it will work? – heringer May 29 '17 at 12:27
  • @heringer: that was missed after first edit, still not working. – Rahul Nagrale May 29 '17 at 12:32
  • Btw.: You can´t call a member within your class, you have to do so within another member. However this has nothing to do with your actual problem concerning `private`. – MakePeaceGreatAgain May 29 '17 at 12:34
  • @RahulNagrale - Read some of the answers on that link I supplied. There are a whole host of reasons why you shouldn't do it, but how you can do it. There is no need to duplicate the answers content. – Darren Wainwright May 29 '17 at 12:35
  • @RahulNagrale, I've added some code - please check my answer – ironstone13 May 29 '17 at 12:59

3 Answers3

1

You can use Reflection to do that, but I would rather avoid calling private members, as it breaks encapsulation and relies on internal implementation details. This will make your code less stable.

Your code could be something like the following:

  1. Get the type you are interested by using Assembly.GetType
  2. Get the method info you would like to execute by using Type.GetMethod
  3. Call the method on the given object by using MethodInfo.Invoke

The code could look something like this:

[TestMethod]
public void TestMethod1()
{
    var type = Assembly.GetExecutingAssembly().GetType("ClassLibrary2.SomeClass");
    var nestedType = type.GetNestedType("SomePrivateClass", BindingFlags.NonPublic);
    var method = nestedType.GetMethod("AddtwoNum", BindingFlags.Static | BindingFlags.NonPublic);
    method.Invoke(null, new object[] { default(int), default(int) });
}
ironstone13
  • 3,325
  • 18
  • 24
0

This way, it works for me:

class SomeClass
{
    private static class SomePrivateClass
    {
        public static int AddtwoNum(int num1, int num2)
        {
            return num1 + num2;
        }
    }
    class SomePublicClass
    {
        private int x = SomePrivateClass.AddtwoNum(1, 2);

        public void InteractWithSomePrivateClass()
        {
            SomePrivateClass.AddtwoNum(1,2); //no problem
        }
    }
}

Same namespace, same assembly, private respected.

As stated by Microsoft,

private members are accessible only within the body of the class or the struct in which they are declared.

So, within the body of the class "SomeClass", the class "SomePrivateClass" is known.

The class "SomePublicClass" is within the body of the class "SomeClass", so it knows the "SomePrivateClass" too.

Note: SomeClass is not really a member, but the definition applies anyway. Here is a more general statement:

private: The type or member can be accessed only by code in the same class or struct.

heringer
  • 2,698
  • 1
  • 20
  • 33
0

I know this is not exactly what you are looking for, but why not mark SomePrivateClass as internal instead of private. Then the class is only visible within the assembly.

Patrick Ribbing
  • 207
  • 2
  • 7