C# - 隐藏的用户控件类的所有方法从衍生的一种 [英] C# - Hiding all methods of the UserControl class from a derived one

查看:486
本文介绍了C# - 隐藏的用户控件类的所有方法从衍生的一种的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



说我有一个名为自定义控件 CustomControl

  > 

当我创建 CustomControl 的一个实例:

  CustomControl cControl =新CustomControl(); 

当我键入 cControl。智能感知提供了我所有的方法和属性用户控件导出。但我想只列出这是在 CustomControl 类来实现我的。



感谢您


解决方案

您可以让一个接口,然后只方法和接口的属性将被暴露出来。

 公共接口ICustomControl {
myProperty的字符串{搞定;设置;}
}

公共类CustomControl:用户控件,ICustomControl {
公共字符串myProperty的{搞定;组; }
}



...

  ICustomControl cControl =新CustomControl(); 



然后智能感知只显示myProperty的和对象的成员(和扩展方法,如果有的话收费)。



编辑:

 保护ICustomControl CustomControl {搞定;组; } 
公共Form1中()
{
的InitializeComponent();
CustomControl = this.customControl1;
CustomControl.MyProperty =!Hello World的; //访问一切都经过这里。
}



然后你就可以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.

Thank you

解决方案

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.

这篇关于C# - 隐藏的用户控件类的所有方法从衍生的一种的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆