<p>Having a heckuva time with this one, though I feel I'm missing something obvious. I have a control that inherits from <code>System.Web.UI.WebControls.Button</code>, and then implements an interface that I have set up. So think...</p>

<pre><code>public class Button : System.Web.UI.WebControls.Button, IMyButtonInterface { ... }
</code></pre>

<p>In the codebehind of a page, I'd like to find all instances of this button from the ASPX. Because I don't really know what the <em>type</em> is going to be, just the <em>interface</em> it implements, that's all I have to go on when looping through the control tree. Thing is, I've never had to determine if an object uses an interface versus just testing its type. <strong>How can I loop through the control tree and yank anything that implements <code>IMyButtonInterface</code> in a clean way</strong> (Linq would be fine)?</p>

<p>Again, know it's something obvious, but just now started using interfaces heavily and I can't seem to focus my Google results enough to figure it out :)</p>

<p><strong>Edit:</strong> <code>GetType()</code> returns the actual class, but doesn't return the interface, so I can't test on that (e.g., it'd return "<code>MyNamespace.Button</code>" instead of "<code>IMyButtonInterface</code>"). In trying to use "<code>as</code>" or "<code>is</code>" in a recursive function, the <em><code>type</code></em> parameter doesn't even get recognized within the function! It's rather bizarre. So</p>

<pre><code>if(ctrl.GetType() == typeToFind) //ok

if(ctrl is typeToFind) //typeToFind isn't recognized! eh?
</code></pre>

<p>Definitely scratching my head over this one.</p>

[{'answer_id': 28662, 'author': 'bdukes', 'author_id': 2688, 'author_profile': 'https://Stackoverflow.com/users/2688', 'pm_score': 1, 'selected': False, 'text': '<p>Interfaces are close enough to types that it should feel about the same.  I\'d use the <a href="http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx" rel="nofollow noreferrer">as operator</a>.</p>\n\n<pre><code>foreach (Control c in this.Page.Controls) {\n    IMyButtonInterface myButton = c as IMyButtonInterface;\n    if (myButton != null) {\n        // do something\n    }\n}\n</code></pre>\n\n<p>You can also test using the <a href="http://msdn.microsoft.com/en-us/library/scekt9xw.aspx" rel="nofollow noreferrer">is operator</a>, depending on your need.</p>\n\n<pre><code>if (c is IMyButtonInterface) {\n    ...\n}\n</code></pre>\n'}, {'answer_id': 28663, 'author': 'Sean Chambers', 'author_id': 2993, 'author_profile': 'https://Stackoverflow.com/users/2993', 'pm_score': 1, 'selected': False, 'text': '<p>Would the "is" operator work?</p>\n\n<pre><code>if (myControl is ISomeInterface)\n{\n  // do something\n}\n</code></pre>\n'}, {'answer_id': 28666, 'author': 'David Basarab', 'author_id': 2469, 'author_profile': 'https://Stackoverflow.com/users/2469', 'pm_score': 3, 'selected': False, 'text': '<p>You can just search on the Interface.  This also uses recursion if the control has child controls, i.e. the button is in a panel.</p>\n\n<pre><code>private List&lt;Control&gt; FindControlsByType(ControlCollection controls, Type typeToFind)\n{\n    List&lt;Control&gt; foundList = new List&lt;Control&gt;();\n\n    foreach (Control ctrl in this.Page.Controls)\n    {\n        if (ctrl.GetType() == typeToFind)\n        {\n            // Do whatever with interface\n            foundList.Add(ctrl);\n        }\n\n        // Check if the Control has Child Controls and use Recursion\n        // to keep checking them\n        if (ctrl.HasControls())\n        {\n            // Call Function to \n            List&lt;Control&gt; childList = FindControlsByType(ctrl.Controls, typeToFind);\n\n            foundList.AddRange(childList);\n        }\n    }\n\n    return foundList;\n}\n\n// Pass it this way\nFindControlsByType(Page.Controls, typeof(IYourInterface));\n</code></pre>\n'}, {'answer_id': 28671, 'author': 'hometoast', 'author_id': 2009, 'author_profile': 'https://Stackoverflow.com/users/2009', 'pm_score': 0, 'selected': False, 'text': "<p>If you're going to do some work on it if it is of that type, then TryCast is what I'd use.</p>\n\n<pre><code>Dim c as IInterface = TryCast(obj, IInterface)\nIf c IsNot Nothing\n    'do work\nEnd if\n</code></pre>\n"}, {'answer_id': 28712, 'author': 'FlySwat', 'author_id': 1965, 'author_profile': 'https://Stackoverflow.com/users/1965', 'pm_score': 0, 'selected': False, 'text': '<p>you can always just use the as cast:</p>\n\n<pre><code>c as IMyButtonInterface;\n\nif (c != null)\n{\n   // c is an IMyButtonInterface\n}\n</code></pre>\n'}, {'answer_id': 28715, 'author': 'Daniel Auger', 'author_id': 1644, 'author_profile': 'https://Stackoverflow.com/users/1644', 'pm_score': 4, 'selected': True, 'text': '<p>Longhorn213 almost has the right answer, but as as Sean Chambers and bdukes say, you should use  </p>\n\n<pre><code>ctrl is IInterfaceToFind\n</code></pre>\n\n<p>instead of  </p>\n\n<pre><code>ctrl.GetType() == aTypeVariable  \n</code></pre>\n\n<p>The reason why is that if you use <code>.GetType()</code> you will get the true type of an object, not necessarily what it can also be cast to in its inheritance/Interface implementation chain. Also, <code>.GetType()</code> will never return an abstract type/interface since you can\'t new up an abstract type or interface. <code>GetType()</code> returns concrete types only.</p>\n\n<p>The reason this doesn\'t work</p>\n\n<pre><code>if(ctrl is typeToFind)  \n</code></pre>\n\n<p>Is because the type of the variable <code>typeToFind</code> is actually <code>System.RuntimeType</code>, not the type you\'ve set its value to. Example, if you set a string\'s value to "<code>foo</code>", its type is still string not "<code>foo</code>". I hope that makes sense. It\'s very easy to get confused when working with types. I\'m chronically confused when working with them.</p>\n\n<p>The most import thing to note about longhorn213\'s answer is that <strong>you have to use recursion</strong> or you may miss some of the controls on the page. </p>\n\n<p>Although we have a working solution here, I too would love to see if there is a more succinct way to do this with LINQ. </p>\n'}, {'answer_id': 28809, 'author': 'Jason Diller', 'author_id': 2187, 'author_profile': 'https://Stackoverflow.com/users/2187', 'pm_score': 2, 'selected': False, 'text': '<p>I\'d make the following changes to Longhorn213\'s example to clean this up a bit:     </p>\n\n<pre><code>private List&lt;T&gt; FindControlsByType&lt;T&gt;(ControlCollection controls )\n{\n    List&lt;T&gt; foundList = new List&lt;T&gt;();\n\n    foreach (Control ctrl in this.Page.Controls)\n    {\n        if (ctrl as T != null )\n        {\n            // Do whatever with interface\n            foundList.Add(ctrl as T);\n        }\n\n        // Check if the Control has Child Controls and use Recursion\n        // to keep checking them\n        if (ctrl.HasControls())\n        {\n            // Call Function to \n            List&lt;T&gt; childList = FindControlsByType&lt;T&gt;( ctrl.Controls );\n\n            foundList.AddRange( childList );\n        }\n    }\n\n    return foundList;\n}\n\n// Pass it this way\nFindControlsByType&lt;IYourInterface&gt;( Page.Controls );\n</code></pre>\n\n<p>This way you get back a list of objects of the desired type that don\'t require another cast to use.  I also made the required change to the "as" operator that the others pointed out. </p>\n'}]
['https://Stackoverflow.com/questions/28642', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1212/']