<p>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. </p>

<p>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.</p>

<p>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.</p>

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

[{'answer_id': 31154, 'author': 'Joel Coehoorn', 'author_id': 3043, 'author_profile': 'https://Stackoverflow.com/users/3043', 'pm_score': 0, 'selected': False, 'text': "<p>If you're worried about forgetting to override the function, then make it abstract.</p>\n"}, {'answer_id': 31208, 'author': 'Lars Truijens', 'author_id': 1242, 'author_profile': 'https://Stackoverflow.com/users/1242', 'pm_score': 1, 'selected': False, 'text': '<p>If you set the Modifiers property of your components to strict protected makes them accessible without the use of a components collection. </p>\n\n<p>Edit:\nDiscoverability could be done using reflection to walk over each field. Although that might be suboptimal in your case.</p>\n'}, {'answer_id': 17007925, 'author': 'Albert', 'author_id': 2090753, 'author_profile': 'https://Stackoverflow.com/users/2090753', 'pm_score': 0, 'selected': False, 'text': "<p>Set the 'components' modifier to protected in your base form class. Remove the 'components' declaration in all the derived forms.</p>\n\n<p>Call this below method in base form load event,</p>\n\n<pre><code>        public void SetComponentsStyle()\n    {\n        if (null != this.components)\n        {\n            foreach (Component comp in this.components.Components)\n            {\n                if (comp is ToolTip)\n                {\n\n                }\n                else if (comp is ContextMenuStrip)\n                {\n\n                }\n            }\n        }\n    }\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/31140', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2899/']