从基本表单访问继承表单上的组件

时间:2022-12-16 15:54:44

A number of forms in my project inherit from a base form. It is easy to get at the Controls collection of the derived forms, but I have not found a simple way to access the Components collection, since VS marks this as private.

我的项目中的许多表单都从基本表单继承。很容易获得派生表单的Controls集合,但我没有找到一种访问Components集合的简单方法,因为VS将其标记为私有。

I assume this could be done with reflection, but I'm not really sure how best to go about it, not having worked with reflection before.

我认为这可以通过反射完成,但我不确定如何最好地解决它,之前没有使用过反射。

Right now, I'm using a sort of clunky workaround, in which I override a function GetComponents and return an array of the components I'm interested in. This is obviously prone to errors, since it's easy to forget to implement the overridden function or update it when components are added.

现在,我正在使用一种笨重的解决方法,我在其中覆盖函数GetComponents并返回我感兴趣的组件数组。这显然容易出错,因为很容易忘记实现重写的函数或者在添加组件时更新它。

If anyone has any tips or can suggest a better way, I'd be glad to hear.

如果有人有任何提示或可以提出更好的方法,我会很高兴听到。

3 个解决方案

#1


1  

If you set the Modifiers property of your components to strict protected makes them accessible without the use of a components collection.

如果将组件的Modifiers属性设置为strict protected,则可以在不使用组件集合的情况下访问它们。

Edit: Discoverability could be done using reflection to walk over each field. Although that might be suboptimal in your case.

编辑:可发现性可以使用反射来遍历每个字段。虽然在您的情况下这可能不是最理想的。

#2


0  

If you're worried about forgetting to override the function, then make it abstract.

如果您担心忘记覆盖该函数,请将其抽象化。

#3


0  

Set the 'components' modifier to protected in your base form class. Remove the 'components' declaration in all the derived forms.

在基础表单类中将“components”修饰符设置为protected。删除所有派生表单中的“组件”声明。

Call this below method in base form load event,

在基本表单加载事件中调用以下方法,

        public void SetComponentsStyle()
    {
        if (null != this.components)
        {
            foreach (Component comp in this.components.Components)
            {
                if (comp is ToolTip)
                {

                }
                else if (comp is ContextMenuStrip)
                {

                }
            }
        }
    }

#1


1  

If you set the Modifiers property of your components to strict protected makes them accessible without the use of a components collection.

如果将组件的Modifiers属性设置为strict protected,则可以在不使用组件集合的情况下访问它们。

Edit: Discoverability could be done using reflection to walk over each field. Although that might be suboptimal in your case.

编辑:可发现性可以使用反射来遍历每个字段。虽然在您的情况下这可能不是最理想的。

#2


0  

If you're worried about forgetting to override the function, then make it abstract.

如果您担心忘记覆盖该函数,请将其抽象化。

#3


0  

Set the 'components' modifier to protected in your base form class. Remove the 'components' declaration in all the derived forms.

在基础表单类中将“components”修饰符设置为protected。删除所有派生表单中的“组件”声明。

Call this below method in base form load event,

在基本表单加载事件中调用以下方法,

        public void SetComponentsStyle()
    {
        if (null != this.components)
        {
            foreach (Component comp in this.components.Components)
            {
                if (comp is ToolTip)
                {

                }
                else if (comp is ContextMenuStrip)
                {

                }
            }
        }
    }