C# 如何隐藏智能感知的公共方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9086136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:50:05  来源:igfitidea点击:

How to hide public methods from intellisense

c#.netreflectionintellisenseencapsulation

提问by Jordan

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 Obsoleteattribute works. Frankly, because I am anal about object encapsulation. I've tried different things, but nothing has actually worked. The method still show up in the member drop-down.

我想从智能感知成员列表中隐藏公共方法。我创建了一个属性,当应用于方法时,将导致在构造其对象时调用该方法。我这样做是为了更好地支持部分类。问题在于,在某些环境(例如 Silverlight)中,反射无法访问私有成员,甚至是子类的私有成员。这是一个问题,因为所有工作都是在基类中完成的。我必须公开这些方法,但我希望它们对智能感知隐藏,类似于Obsolete属性的工作方式。坦率地说,因为我对对象封装很感兴趣。我尝试了不同的东西,但实际上没有任何效果。该方法仍然显示在成员下拉列表中。

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 realquestion, Philistines! This can also apply to MEF properties that have to be public though sometimes you want to hide them from clients.

当我不希望客户端调用公共方法时,如何防止公共方法出现在智能感知中?这是一个真正的问题,非利士人!这也适用于必须公开的 MEF 属性,但有时您想对客户端隐藏它们。

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

更新:自从我发布这个问题以来,我作为一名开发人员已经成熟了。为什么我如此关心隐藏界面是我无法理解的。

采纳答案by cadrell0

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

扩展我对部分方法的评论。尝试这样的事情

Foo.part1.cs

foo.part1.cs

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

    partial void Initialize();
}

Foo.part2.cs

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
    }
}

回答by Adi Lester

Using the EditorBrowsableattribute like so will cause a method not to be shown in intellisense:

EditorBrowsable像这样使用属性会导致一个方法不会在智能感知中显示:

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

回答by SimSimY

You are looking for EditorBrowsableAttribute

你正在寻找 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.

下面的示例演示了如何通过为 EditorBrowsableAttribute 属性设置适当的值来从 IntelliSense 隐藏类的属性。在它自己的程序集中构建 Class1。

在 Visual Studio 中,创建一个新的 Windows 应用程序解决方案,并添加对包含 Class1 的程序集的引用。在 Form1 构造函数中,声明 Class1 的一个实例,键入实例的名称,然后按句点键以激活 Class1 成员的 IntelliSense 下拉列表。Age 属性不会出现在下拉列表中。

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;
                }
            }
        }
    }
}