发现在ASP.NET中使用特定的界面控件 [英] Finding controls that use a certain interface in ASP.NET

查看:153
本文介绍了发现在ASP.NET中使用特定的界面控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个heckuva的时间与这一个,虽然我觉得我失去了一些东西明显。我有一个从继承的控制System.Web.UI.WebControls.Button ,然后实现了我已成立了一个接口。因此,认为...

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

public class Button : System.Web.UI.WebControls.Button, IMyButtonInterface { ... }

在页面的codebehind,我想找到从ASPX这个按钮的所有实例。因为我真的不知道是什么的键入的将是,刚刚的接口的它实现了,这就是我必须去通过控制树循环时。事情是,我从来没有来确定一个对象使用而不只是测试它的类型的接口。 如何通过控制树环和猛拉任何实现 IMyButtonInterface 以干净的方式(LINQ将被罚款)?

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 type is going to be, just the interface 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. How can I loop through the control tree and yank anything that implements IMyButtonInterface in a clean way (Linq would be fine)?

再次知道它的东西很明显,但刚才使用的接口在很大程度上开始,我似乎无法集中我的谷歌的结果足以看着办吧:)

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 :)

编辑: 的GetType()返回实际的类,但不返回的接口,所以我不能对测试(例如,它会返回 MyNamespace.Button 而不是 IMyButtonInterface )。在尝试使用在递归函数时,的 键入 的参数甚至不会在函数内部认可!这是相当奇特。因此,

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

if(ctrl.GetType() == typeToFind) //ok

if(ctrl is typeToFind) //typeToFind isn't recognized! eh?

肯定抓我的头在这一个。

Definitely scratching my head over this one.

推荐答案

Longhorn213几乎有正确的答案,但肖恩钱伯斯和bdukes说,你应该使用

Longhorn213 almost has the right answer, but as as Sean Chambers and bdukes say, you should use

ctrl is IInterfaceToFind

而不是

ctrl.GetType() == aTypeVariable  

之所以是,如果你使用 .GetType()你会得到真正的类的一个对象,不一定是它也可以在其继承被转换为/接口实现连锁。此外, .GetType()将永远不会返回一个抽象类型/接口,因为你不能将新建立一个抽象类型或接口。 的GetType()返回的具体类型只。

The reason why is that if you use .GetType() 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, .GetType() will never return an abstract type/interface since you can't new up an abstract type or interface. GetType() returns concrete types only.

究其原因,这并不正常工作

The reason this doesn't work

if(ctrl is typeToFind)  

是因为变量的类型 typeToFind 实际上 System.RuntimeType ,不是那种你已经设置它的价值。例如,如果你设置一个字符串的值改为,它的类型仍然是字符串不是。我希望是有道理的。这是很容易与类型工作时感到困惑。与他们一起工作的时候,我长期糊涂了。

Is because the type of the variable typeToFind is actually System.RuntimeType, not the type you've set its value to. Example, if you set a string's value to "foo", its type is still string not "foo". I hope that makes sense. It's very easy to get confused when working with types. I'm chronically confused when working with them.

最进口的事情需要注意的longhorn213的回答是,您必须使用递归或你可能会错过一些页面上的控件。

The most import thing to note about longhorn213's answer is that you have to use recursion or you may miss some of the controls on the page.

虽然我们在这里有一个工作的解决方案,我也很想看看是否有与LINQ做到这一点更简洁的方式。

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.

这篇关于发现在ASP.NET中使用特定的界面控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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