Help solve the problem: class A is inherited from class B, a class C has one method that should be available only to class A. How can this be implemented using C # (.NET 3.5)?


Class C is my singleton that is initialized once with class A (a parameter is passed from class A), and I would like to make sure that other classes know nothing about that method with a parameter in class C that creates this single instance of class C, and for them only the method for receiving a singleton object was available.


class A is a class in the Global.asax file (a kind of global application class) so I was told that I would load data for the logged-in user during authentication (with the condition that they are not loaded into the session)

    3 answers 3

    In C #, there is no direct equivalent of the friend declaration from C ++. However, you can try alternatives:

    1. Why should class A generally have access to private methods of class C if they have nothing to do with each other? Could it really be that class A is an internal class of class C ? Declare it inside, there will be access to internal methods.

    2. Maybe class A is part of a test system, is it testing class C ? Then declare the desired method as internal , and place the InternalsVisibleTo attribute on the main assembly that references the test assembly.

    3. If absolutely no other way is possible, use reflection.


    In general, explain what you want to achieve semantically, it may well be that you are going the wrong way.

    • one
      @ minusator: do not take the trouble to explain your minus. - VladD
    • 2
      Haters in their style, they will not explain. - nitrocaster
    • Class C is my singleton, which is initialized once with class A (a parameter is passed from class A), and I would like to make sure that other classes know nothing about that method with a parameter in class C that creates this single instance of class C, and for them only the method for receiving a singleton object was available. - romandrovich
    • @romandrovich: it’s usually a bad idea to allow a class to know too much about other classes, encapsulation is lost, that is, the whole point of classes. So tell me why in order to initialize from A you need access to C for A Is class A essentially a “piece” of C ? Then so declare it. If A has an application outside of C initialization, he will be able to do too much, break it apart. - VladD
    • Make a lazy constructor by pulling the value from the class A property when the object is created. - Sofver

    No This is a bad design, most likely.

      Class "C" is inherited from any of these classes? If yes, hang protected on the method that you want to pull from "A", if not - make it easier - make an object of class "A" as a field of class "C" and pull whenever you want.

      • No, on the contrary, I should inherit A from C and pull a method from C from A, but you cannot inherit since no multiple inheritance! - romandrovich
      • If the method does not work with the internal fields of class "C", make an Interface that implements the method you need and follow for classes "A" and "C" - Sofver