If everything is within the same class (only with the comments that this became clear) to local function is the solution. Obviously it is limited to a method, there is no way to make a method be accessed by a list of methods.
Everything has to be within the scope or specified the scope in a general, non-specific way, unless you make an external tool that you have to go through throughout the compilation. One of the reasons for the creation of .NET Compiler Platform is just making this kind of tool. But of course if you don’t go through it won’t prohibit anything.
Remembering that forcing or prohibiting something always occurs when the programmer wants to follow the standard protocols, if he wants to go over the top he always succeeds. This would be protection against inadvertent error, not security against fraud.
If the local function does not meet, the maximum that the default compiler allows is to ban your call outside the class (private
), or outside the class hierarchy (protected
) or outside the Assembly (internal
) or a combination of the latter two, or may limit further and allow a class method to be the only one to access a function. But you can’t nominally say who can access.
It is possible to say that can be accessed by some other Assembly specific with an attribute.
This is all mixed up in the same class ?
– Rovann Linhalis
Yes, it’s all within one class
– Leonardo Bonetti
No. Perhaps the solution to the problem you are facing is another one. We may try to help through a context and a [mcve]
– Diego Rafael Souza
@Diegorafaelsouza you want a more verifiable example than that?
– Leonardo Bonetti
@Leonardobonetti what you need are
local functions
, declared functions of a method that are only available within the scope of that method.– Omni
@Diegorafaelsouza Explain to me which points of my example disagree with the M.C.V example.
– Leonardo Bonetti
You are starting from what you believe to be a solution. My proposal was that you change the perspective of the question, starting from the problem.
– Diego Rafael Souza
There is no problem in my code, it is a question whether or not there is a resource, there is no solution to something that does not have a problem, has a doubt.
– Leonardo Bonetti
what Diego meant, is for you to explain what the need of the "son method could only be called exclusively by the father method" because for this need there can be other solutions...
– Rovann Linhalis
@Rovannlinhalis now yes understood, I want to delimit not allow this method to be called by others as a form of organization and prevention that another programmer (or even me) use the method in the future because it only exists according to the parent method.
– Leonardo Bonetti
Maybe it’s something pretty useless, but I believe that so the code gets more organized, I may be tremendously wrong too.
– Leonardo Bonetti
right, I think the most appropriate thing would be to organize it into classes... Or if you have to use the function more than once within the parent method... you can use the function that Niero put in the answer, And if you’re only going to use it once, I don’t even see why you’d be inside another function...
– Rovann Linhalis