相关疑难解决方法(0)

检查类是否派生自泛型类

我的项目中有一个派生类的泛型类.

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)

不起作用.

c# generics reflection

287
推荐指数
11
解决办法
14万
查看次数

typeof和is关键字有什么区别?

这两者之间的确切区别是什么?

// 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)

c# generics types

42
推荐指数
6
解决办法
4051
查看次数

标签 统计

c# ×2

generics ×2

reflection ×1

types ×1