C# - Hiding all methods of the UserControl class f

2019-06-06 04:24发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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.



回答5:

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.



回答6:

Don't inherit UserControl, inherit Control instead.



回答7:

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.