How to hide public methods from IntelliSense

29,255

Solution 1

To expand on my comment about partial methods. Try something like this

Foo.part1.cs

partial class Foo
{
    public Foo()
    {
        Initialize();
    }

    partial void Initialize();
}

Foo.part2.cs

partial class Foo
{
    partial void Initialize()
    {
         InitializePart1();
         InitializePart2();
         InitializePart3();
    }

    private void InitializePart1()
    {
        //logic goes here
    }

    private void InitializePart2()
    {
        //logic goes here
    }

    private void InitializePart3()
    {
        //logic goes here
    }
}

Solution 2

Using the EditorBrowsable attribute like so will cause a method not to be shown in IntelliSense:

[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public void MyMethod()
{
}

Solution 3

You are looking for EditorBrowsableAttribute

The following sample demonstrates how to hide a property of a class from IntelliSense by setting the appropriate value for the EditorBrowsableAttribute attribute. Build Class1 in its own assembly.

In Visual Studio, create a new Windows Application solution, and add a reference to the assembly which contains Class1. In the Form1 constructor, declare an instance of Class1, type the name of the instance, and press the period key to activate the IntelliSense drop-down list of Class1 members. The Age property does not appear in the drop-down list.

using System;
using System.ComponentModel;

namespace EditorBrowsableDemo
{
    public class Class1
    {
        public Class1()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        int ageval;

        [EditorBrowsable(EditorBrowsableState.Never)]
        public int Age
        {
            get { return ageval; }
            set
            {
                if (!ageval.Equals(value))
                {
                    ageval = value;
                }
            }
        }
    }
}
Share:
29,255
Jordan
Author by

Jordan

Updated on July 08, 2022

Comments

  • Jordan
    Jordan almost 2 years

    I want to hide public methods from the IntelliSense member list. I have created an attribute that, when applied to a method, will cause the method to be called when its object is constructed. I've done this to better support partial classes. The problem is that in some environments (such as Silverlight), reflection cannot access private members, even those of child classes. This is a problem since all of the work is done in a base class. I have to make these methods public, but I want them to be hidden from IntelliSense, similar to how the Obsolete attribute works. Frankly, because I am anal about object encapsulation. I've tried different things, but nothing has actually worked. The method still shows up in the member drop-down.

    How do I keep public methods from showing up in IntelliSense when I don't want them to be called by clients? How's that for a real question, Philistines! This can also apply to MEF properties that have to be public though sometimes you want to hide them from clients.

    Update: I have matured as a developer since I posted this question. Why I cared so much about hiding interface is beyond me.

  • Jordan
    Jordan over 12 years
    Yeah, I tried this. It doesn't work. The method still shows in the member list.
  • Adi Lester
    Adi Lester over 12 years
    @Jordan I tried it myself just now and it doesn't show the method when I'm referencing the library's dll. However, it does show if the project is in the same solution and I'm referencing the project.
  • Cody Gray
    Cody Gray over 12 years
    Yes, that's the expected behavior. It's not intended to filter items from you as you write the code!
  • Jordan
    Jordan over 12 years
    Indeed. I'm breaking OOP rules for the sake of cleaner code. I'm suffering from software OCD or something.
  • Jordan
    Jordan over 12 years
    This is ultimately what I did. sigh
  • Jordan
    Jordan over 12 years
    Well, not exactly. I just used private methods in the partial classes that I called from the primary class. I have multiple partial classes, I'm crazy. Need help. sigh
  • Isaac Bolinger
    Isaac Bolinger about 12 years
    editorbrowsable only hides the method if you're just importing the dll, not if you're referencing another project in the solution i think
  • Christoph
    Christoph over 5 years
    It used to work in VB.Net and older Visual Studio versions but then they changed it and incorporated the incorrect behavior of C#, where it is not respected anymore if the project is in the same solution. And there is not even an option to choose the behavior. :-(