Francesco
Francesco

Reputation: 10820

Accessing a private method from another class

I have two repository classes (RepositoryFactory and BaseRepository) implementing different interfaces within the same project. The BaseRepository class has a private method that would be now needed also in the other class, with the same implementation.

Instead of duplicate the method in order to keep it private, I was thinking to a possible alternative, although so far I could not find a good solution since by definition a private method has scope only in its own class.

Using inheritance and change the method to "protected" would also not be an option, since the two classes are not linked semantically. I cannot use a public property giving back the result of the method since the return type is void.

Upvotes: 9

Views: 30963

Answers (4)

Dilhara Peiris
Dilhara Peiris

Reputation: 1

Reference

Possible by using reflection

  1. Create a console application in Visual Studio.
  2. Add 2 namespaces
    2.1. System
    2.2. System.Reflection
  3. Now create a class and inside that class create one method that will be private as follows:

Upvotes: 0

Jaime Botero
Jaime Botero

Reputation: 2311

You can use reflection. Here's an example:

MethodInfo privMethod = objInstance.GetType().GetMethod("PrivateMethodName", BindingFlags.NonPublic | BindingFlags.Instance);
privMethod.Invoke(objInstance, new object[] { methodParameters });

Upvotes: 20

João Angelo
João Angelo

Reputation: 57688

You can, but it looks awkward. This takes advantage of nested classes being able to access private stuff from the containing class. However, even if something is possible doesn't mean you should do it. If you just change the modifier to internal you get the same behavior and since the two classes are coupled together then it makes sense to ship them in the same assembly, so internal modifier is the correct answer.

public class BaseRepository
{
    public sealed class RepositoryFactory
    {
        public static BaseRepository Create()
        {
            var repo = new BaseRepository();

            repo.MethodRequiredByRepositoryFactory();

            return repo;
        }
    }

    private void MethodRequiredByRepositoryFactory() { }
}

Upvotes: 4

Roman Starkov
Roman Starkov

Reputation: 61402

It's not possible to do what you want in C#. The closest you can have is internal, which makes the member visible to an entire assembly. It might also be possible to make the two classes private and nested inside another class, but this isn't always appropriate.

Mads Torgersen, who works on C#, has this to say about it:

I've seen a number of proposals trying to grapple with some notion of "class set accessibility." The complication of course is that, unlike existing accessibilities, there is not already a natural group (everyone, assembly, derived classes, single class) to tie it to, so even with another accessibility modifier you still also need syntax (or something) to define the group.

There are several ways to slice it. I haven't seen a proposal that is obviously right, but I think the problem is relevant, and I will take this up with the design team.

(source)

Upvotes: 9

Related Questions