C# - 从派生类中隐藏 UserControl 类的所有方法

发布于 2024-08-11 22:15:20 字数 494 浏览 2 评论 0原文

我有一个自定义用户控件。通常它继承UserControl类。但通过这种方式它继承了UserControl的所有公共方法和属性。但我想隐藏所有这些并实现我自己的一些方法和属性。

假设我有一个名为 CustomControl 的自定义控件。

public class CustomControl : UserControl

当我创建 CustomControl 的实例时:

CustomControl cControl = new CustomControl();

当我输入 cControl. 时,intellisense 为我提供从 UserControl 派生的所有方法和属性。但我只想列出在 CustomControl 类中实现的我的。

I have a custom user control. Normally it inherites the UserControl class. But by this way it inherites all the public methods and properties of UserControl. But I want to hide all these and implement my own few methods and properties.

Say that I have a custom control named CustomControl.

public class CustomControl : UserControl

When I create an instance of CustomControl:

CustomControl cControl = new CustomControl();

When I type cControl. intellisense offers me all the methods and properties derived from UserControl. But I want to list only mine which are implemented in CustomControl class.

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

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

发布评论

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

评论(7

惜醉颜 2024-08-18 22:15:20

您可以创建一个接口,然后只公开该接口的方法和属性。

public interface ICustomControl {
     string MyProperty { get; set;}
}

public class CustomControl : UserControl, ICustomControl {
     public string MyProperty { get; set; }
}

...

ICustomControl cControl = new CustomControl();

然后智能感知仅显示 MyProperty 和 Object 的成员(以及扩展方法,如果有的话)。

编辑:

protected ICustomControl CustomControl { get; set; }
    public Form1()
    {
        InitializeComponent();
        CustomControl = this.customControl1;
        CustomControl.MyProperty = "Hello World!"; // Access everything through here.
    }

然后,如果需要,您可以将 CustomControl 的范围设为内部或受保护的内部。

You could make an interface, and then only the methods and properties of the interface will be exposed.

public interface ICustomControl {
     string MyProperty { get; set;}
}

public class CustomControl : UserControl, ICustomControl {
     public string MyProperty { get; set; }
}

...

ICustomControl cControl = new CustomControl();

Then intellisense only shows MyProperty and Object's members (and extension methods, if any apply).

EDIT:

protected ICustomControl CustomControl { get; set; }
    public Form1()
    {
        InitializeComponent();
        CustomControl = this.customControl1;
        CustomControl.MyProperty = "Hello World!"; // Access everything through here.
    }

Then you can make CustomControl's scope internal or protected internal if you want.

公布 2024-08-18 22:15:20

继承不是这样运作的。通过创建子类,您明确表示您希望基类的所有方法和属性都可访问。

That's not how inheritance works. By creating a sub class you are explicitly saying you want all of the base class's methods and properties accessible.

明天过后 2024-08-18 22:15:20

为什么不使用组合并使 UserControl 成为自定义控件的成员而不是从它继承?

Why not use composition and make the UserControl a member of your custom control instead of inheriting from it?

时光礼记 2024-08-18 22:15:20

您可以通过在类中隐藏它们(使用关键字 new 重新声明每个继承的方法)并向它们应用 EditorBrowsableAttribute 来对 IntelliSense 隐藏它们。但这些方法仍然存在,并且仍然可以调用。一般来说,没有办法禁止客户端调用类实例上的继承方法。

You can hide them from IntelliSense by shadowing them in your class (redeclaring each inherited method with keyword new), and applying EditorBrowsableAttribute to them. But the methods will still be there, and will still be callable. In general, there is no way to disallow clients to call inherited methods on instances of your class.

七度光 2024-08-18 22:15:20

您有一些选择:

  1. 使用 EditorBrowsableAttribute 将从智能感知中隐藏属性
  2. 使用 BrowsableAttribute 将从属性网格中隐藏属性
  3. 使用“private new”隐藏属性本身将从所有内容中隐藏它们

需要考虑的事情:

  1. 使用属性将隐藏属性,具体取决于“消费者”的实现,但在语言级别,您没有隐藏任何内容。例如,您可以实现一个属性网格,在显示属性之前检查 EditorBrowsableAttribute。 [我不确定 Microsoft 的 Windows 窗体实现]
  2. 使用“private new”也会阻止您访问属性,尽管在控件内部您仍然可以调用 base.PropertyName 来访问属性原来的。

我明白你的意图。通常会限制继承控件的行为,即使它“破坏”了继承概念。

You have some options:

  1. Using EditorBrowsableAttribute will hide the properties from intellisense
  2. Using BrowsableAttribute will hide the properties from property grids
  3. Hiding the properties themselves using "private new" will hide them from everything

Things to consider:

  1. Using attributes will hide the properties depending on the implementation of the "consumer" but in a language level, you aren't hiding anything. For instance, you could implement a property grid that checks for EditorBrowsableAttribute before showing the property. [I'm not sure about Microsoft's Windows Forms implementation]
  2. Using "private new" will also prevent you to access the properties, although, inside your control you can yet call base.PropertyName to have access to the original ones.

I understand your intention. It's usual to restrict behavior of enherited controls even though it "breaks" the inheritance concept.

2024-08-18 22:15:20

不要继承UserControl,而是继承Control

Don't inherit UserControl, inherit Control instead.

烛影斜 2024-08-18 22:15:20

您可以通过聚合来做到这一点,例如

public CustomControl
{
    private Control control_;
    public property control {get{ return _control;}}
    .
    .
    .
    public void FunctionIWantExposed() {}
}

但这并不是特别有用。您将无法将其添加到任何控件集合中。您可以使用自定义类控件中的属性并将其添加到控件集合中,但随后您尝试隐藏的所有这些方法都会再次暴露。

You could do this through aggregation e.g.

public CustomControl
{
    private Control control_;
    public property control {get{ return _control;}}
    .
    .
    .
    public void FunctionIWantExposed() {}
}

But this isn't particularly helpful. You won't be able to add it to any controls collection. You could use the property in the custom class control and add that to a controlscollection but then all those methods you are trying to hide get exposed again.

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