【发布时间】:2020-12-21 05:49:57
【问题描述】:
考虑以下检查对象是否属于特定类型的代码:
public MyMethod(object myObject)
{
if (myObject.GetType() != typeof(MyClass))
{
throw new ArgumentException("The type is not MyClass");
}
}
如何检查myObject 的类型,以便它允许MyClass 的子类型?
【问题讨论】:
-
它似乎工作,但是有一个警告:
The given expression is never of the provided ('MyClass') type。
标签: c# object inheritance types