如何从 IntelliSense 中隐藏公共方法

发布于 2025-01-01 07:56:30 字数 486 浏览 1 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2025-01-08 07:56:30

使用 EditorBrowsable 属性像这样会导致方法不显示在 IntelliSense 中:

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

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()
{
}
北方。的韩爷 2025-01-08 07:56:30

您正在寻找 EditorBrowsableAttribute

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

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

使用系统;
使用 System.ComponentModel;

命名空间 EditorBrowsableDemo
{
    公开课Class1
    {
        公共类1()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        int 年龄;

        [EditorBrowsable(EditorBrowsableState.Never)]
        公共整数年龄
        {
            获取{返回年龄值; }
            放
            {
                if (!ageval.Equals(value))
                {
                    年龄值=值;
                }
            }
        }
    }
}

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;
                }
            }
        }
    }
}
浅笑依然 2025-01-08 07:56:30

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

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

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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文