【发布时间】:2016-02-07 07:24:08
【问题描述】:
我有一种方法,
public function DoSomethingGenricWithUIControls(ByVal incomingData As Object)
//Fun Stuff
End Function
这个方法会被调用并且可以通过Page,UserControl,或者任何其他类型。
我想检查传入对象的类型,是Page,UserControl还是其他类型。
但我无法做到这一点。每当我尝试在System.Web.UI.UserControl 上使用typeOf() 的GetType() 时。它给了,
'UserControl' is a type in 'UI' and cannot be used as an expression.
当我尝试其他方法时,例如 .IsAssignableFrom() 和 .IsSubclassOf(),但我仍然无法做到这一点。
另外请注意,我传入的usercontrols 或page 可以从不同的控件/页面进行多重继承。所以它的直接基类型不是System.Web.UI.<Type>。
如果有任何混淆,请告诉我。 VB/C# 任何方式都适合我。
更新
我试过了,
if( ncomingPage.GetType() Is System.Web.UI.UserControl)
这给了我与上述相同的问题,
'UserControl' is a type in 'UI' and cannot be used as an expression.
【问题讨论】:
-
我不想陷入
try catch铸造,这不是一个好习惯,我已经尝试过Is,但仍然是同样的问题 -
在使用
as/nullcheck 时不需要try/catch。你能用is发布不工作的代码吗?
标签: c# vb.net reflection types