谁能帮我?我无法弄清楚我做错了什么,但似乎会有一个简单的解决方案:
通常你可以is像这样使用:
if (theObject is MyClass) ...
Run Code Online (Sandbox Code Playgroud)
但是如果你想指定它在运行时检查的类型,那么就不会编译
Type theType = ...
if (theObject is theType) ...
Run Code Online (Sandbox Code Playgroud)
我试过这样做:
if (theObject.GetType() == theType) ...
Run Code Online (Sandbox Code Playgroud)
但这只有theType在确切类型的情况下才有效,并且不会像is语句那样考虑继承
我确定存在一个解决方案(可能使用泛型),但我现在想不到一个(它突然记得你点击'发布'时如何做的事情)
lad*_*dge 14
听起来你想要IsAssignableFrom(),就像在
if (theType.IsAssignableFrom(theObject.GetType())) ...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
163 次 |
| 最近记录: |