我的项目中有一个派生类的泛型类.
public class GenericClass<T> : GenericInterface<T>
{
}
public class Test : GenericClass<SomeType>
{
}
Run Code Online (Sandbox Code Playgroud)
有没有办法找出一个Type对象是否来自GenericClass?
t.IsSubclassOf(typeof(GenericClass<>))
Run Code Online (Sandbox Code Playgroud)
不起作用.
这两者之间的确切区别是什么?
// When calling this method with GetByType<MyClass>()
public bool GetByType<T>() {
// this returns true:
return typeof(T).Equals(typeof(MyClass));
// this returns false:
return typeof(T) is MyClass;
}
Run Code Online (Sandbox Code Playgroud)