Private method?

In my projects, I often want to have a method (or function, if you prefer), but I also want to access it from one other class. Is it possible?

To clarify, this method can be accessed from ClassA , its own class, but not any other.

+5
source share
7 answers

There are many ways to do this before your last statement, which is β€œand this class ONLY”, I can only think of one way to do this, and it should have classes laid out in assemblies, such as those:

Assembler A contains only class A using the method you want to declare as internal

Assembly B is declared as a friendly assembly: https://msdn.microsoft.com/en-us/library/0tke9fxk.aspx and contains the code to call A (it can, because it is internal, as if in the same assembly as and assembly of friends)

No other assembly related to A, B, or both, can call a class A method, since it is internal.

+2
source

You can protect this method if it matches your OOP structure:

 public class A { protected void Test() { Console.WriteLine("I can only be called from B"); } } public class B : A { public void Pub() { Test(); } } 

And there are many other ways to do this.

However, in general, this sounds like a wrong look at access modifiers.
If you only want to call your method from only one place, just call it from only one place.
The fact that this method should be called from another class makes it public, logical, and architectural.

+2
source

The best way I can think of is this.

A set of caller information attributes has been added to C # 5, namely [System.Runtime.CompilerServices.CallerMemberName] , [System.Runtime.CompilerServices.CallerFilePath] and [System.Runtime.CompilerServices.CallerLineNumber] . We can use CallerFilePathAttribute to find out if the caller comes from a specific .cs file.

Typically, a single file contains only one class or structure. For example, ClassA is defined in ClassA.cs . You can check if the caller's ClassA.cs method.

So, change the parameters of your method as follows:

 ([CallerFilePath] string callerFilePath = "" /*Other parameters*/) 

In the method, check if the path to the ClassA file ClassA . If this is not the case, write an exception indicating that access to the method can only be obtained from ClassA !

+2
source

Everything you ask for is not possible in C #. I mean, you cannot use only one class to use a private method. All you can do is use internal , which allows classes of only one assembly to access your methods or protected , which are available in its class and instances of the derived class!

Also, there is no thumb rule for what you ask for, but you can do some hacking as shown below:

 MethodInfo privateMethod = instance.GetType().GetMethod("NameOfPrivateMethod", BindingFlags.NonPublic | BindingFlags.Instance); privateMethod.Invoke(instance, new object[] { methodParameters }); 
+1
source

Another easy way to control member access is to use delegates. Suppose you have a private method:

 class SecureMethod { private void DoSomething() { } } 

You can grant access to this method by inserting a delegate to this method:

 class ClassA { public ClassA(Action secureMethod) { } } SecureMethod objWithSecureMethod; var a = new ClassA( objWithSecureMethod.DoSomething ); 
+1
source

I am showing you how to do this, but these are very bad practices:

 public class A { private void CanOnlyCallMethodInClassB(); public static void SetHandlerCanOnlyCallMethodInClassB(ClassB b) { b.MethodFromClassA = CanOnlyCallMethodInClassB; } } public class B { public Action MethodFromClassA { get; set; } } 

in code:

 var b = new B(); A.SetHandlerCanOnlyCallMethodInClassB(b); b.MethodFromClassA(); 

but it’s better to use an object of class ClassB in class methodA. Search google for the strategy template or use inheritance .

+1
source

One slightly dirty trick to use a class B method in class A is to make the method secure, not private, and get A from B.

Another possibility, perhaps better, is to make the method internal rather than private, and then put the classes A and B in the same assembly.

0
source

Source: https://habr.com/ru/post/1239193/


All Articles