Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列出一个类';什么是泛型超类?_C#_Generics_Inheritance_Reflection - Fatal编程技术网

C# 列出一个类';什么是泛型超类?

C# 列出一个类';什么是泛型超类?,c#,generics,inheritance,reflection,C#,Generics,Inheritance,Reflection,我有一个继承层次结构: public interface IValidator<TItem> public abstract class Validator<TItem, TId> : IValidator<IItem> public class MyValidator : Validator<MyItem, int> 但是当我对超类尝试相同的方法时,我得到的只是System.Object,而不是Validator 如何获得完整的继承层次结构

我有一个继承层次结构:

public interface IValidator<TItem>

public abstract class Validator<TItem, TId> : IValidator<IItem>

public class MyValidator : Validator<MyItem, int>
但是当我对超类尝试相同的方法时,我得到的只是
System.Object
,而不是
Validator


如何获得完整的继承层次结构?

您可以检查基类型,而不是迭代程序集中的每个类型。另外,您不需要使用
MyValidator
的实例来获取
MyValidator
类型。检查此代码:

var baseType = typeof(MyValidator).BaseType;
var baseTypes = new List<Type> { baseType };

while(baseType != typeof(object))
    baseTypes.Add(baseType = baseType.BaseType);
var baseType=typeof(MyValidator).baseType;
var baseTypes=新列表{baseType};
while(baseType!=typeof(object))
添加(baseType=baseType.baseType);

现在
baseTypes
列表包含
Validator
object
类型。

a.GetTypes()
不获取构造类型。但是为什么不使用
typeof(MyValidator)。BaseType
?while循环是什么<代码>MyValidator只能有一个基class@Z.R.T.当然可以,但是
MyValidator
的基类可能有基类来帮助您!我知道这一定很容易,但每次我都错过了。
var type = validator.GetType();
return AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(a => a.GetTypes())
    .Where(supertype => type.IsSubclassOf(supertype));
var baseType = typeof(MyValidator).BaseType;
var baseTypes = new List<Type> { baseType };

while(baseType != typeof(object))
    baseTypes.Add(baseType = baseType.BaseType);