Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6544099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:25:32+00:00 2026-05-25T11:25:32+00:00

I have a method that I want to access from only one method because

  • 0

I have a method that I want to access from only one method because it is very specific and could confuse the programmers if they “see” that method from the other parts of the class. On the other hand, for unit testing and for readability, I have to make that method.

Is there any trick to encapsulate further than usual?

Just to make sure my idea is clear: method A calls method B. I want intellisense to show method B only if I am in method A.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:25:32+00:00Added an answer on May 25, 2026 at 11:25 am

    You can check the caller of the method, execute your method body only if the caller name matches (assuming you have only 1 method of that name, otherwise you have to check the arguments as well).

    Not possible to hide this in intellisense though. Furthermore create a region in the class for the DO NOT USE method.

    private void Func3()
    {
        // Will not execute the method body
        Func2();
    }
    
    private void Func1()
    {
        // Will execute the method body
        Func2();
    }
    
    private void Func2()
    {
        var callingMethodName = new StackFrame(1, true).GetMethod().Name;
        if (string.Equals(callingMethodName, "Func1"))
        {
            // Execute your method's code....
        }
    }
    

    An alternative approach using Attributes

    Rather than hard coding the method name, you can create a custom attribute and mark your method with it to indicate that you are allowing this caller to execute your DoNotUseMethod().

    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
    class AllowedMethodAttribute : Attribute
    {
        public AllowedMethodAttribute(bool methodAllowed)
        {
            this.AllowedToExecute = methodAllowed;
        }
    
        public bool AllowedToExecute { get; private set; }
    }
    

    And in your class use the attribute as follows,

    private void AnotherNotAllowedMethod()
    {
        MyDoNotUseMethod();
    }
    
    [AllowedMethod(false)]
    private void NotAllowedMethod()
    {
        MyDoNotUseMethod();
    }
    
    [AllowedMethod(true)]
    private void MyAllowedMethod()
    {
        MyDoNotUseMethod();
    }
    
    #region DoNotUseMethod 
    
    private void MyDoNotUseMethod()
    {
        var callingMethod = new StackFrame(1, true).GetMethod();
        var allowedMethodAttributes = callingMethod.GetCustomAttributes(typeof (AllowedMethodAttribute), false);
        if(allowedMethodAttributes.Length == 1)
        {
            var allowedMethodAttribute = allowedMethodAttributes[0] as AllowedMethodAttribute;
            if(allowedMethodAttribute != null && allowedMethodAttribute.AllowedToExecute)
            {
                // Execute the method body
            }
            else
            {
                Debug.Assert(false, callingMethod.Name + " is not allowed to execute this method", 
                    "Mark method with AllowedMethod(true) attribute to allow execution of this method");
                // throw exception if required
            }
        }
        else
        {
            Debug.Assert(false, callingMethod.Name + " is not allowed to execute this method", 
                "Mark method with AllowedMethod(true) attribute to allow execution of this method");
            // throw exception if required
        }
    }
    
    #endregion
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would I do if I want to have a generic method that only
I have a method that where I want to redirect the user back to
I have a method: myMethod() {} that I want to make accessible to javascript.
I have a method CreateAccount(...) that I want to unit test. Basically it creates
Say I want to have a method that takes any kind of number, is
I have a method that's about ten lines of code. I want to create
I have a method that returns a list of type string. I want to
I have a method running on the EDT and within that I want to
I have a method song_link that calls link_to internally. I want the caller to
I have a class with a ToString method that produces XML. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.