gpt4 book ai didi

c# - 检查动态对象是否具有 Foo 的基础

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:30 25 4
gpt4 key购买 nike

我们有这两个类

class Foo{
public Foo(){}
}

class FooBar : Foo{
public FooBar : base() {}
}

我知道你可以看看动态对象的类型是不是类似

dynamic bar = new FooBar();
bool isType = bar is FooBar;

但是我如何检查 bar 是否是 foo 类型呢?

如在

dynamic bar = new FooBar();
//This would need to check the base as well
bool isType = bar is Foo;

或者这已经有效了吗?

最佳答案

是的,那已经可以工作了。 is 只是检查对象是否可以转换为给定类型。请参阅其上的文档 here .

关于c# - 检查动态对象是否具有 Foo 的基础,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14794021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com