简体   繁体   中英

How to know the base type of an inherited generic type?

Consider this code :

class MyClass<T>
{
}

class AnotherClass : MyClass<String>
{
}

When I look at the BaseType property of the AnotherType Type, it says that it is Object, where I expected to see the generic MyClass type.

Is there a way to know that AnotherClass inherits MyClass ?

EDIT : The problem was that the MyClass type was actually an interface, so it is totally normal that it is not shown as BaseType.

Unable to reproduce:

using System;

class MyClass<T> {}
class AnotherClass : MyClass<string> {}

public class Test
{
    static void Main()
    {
        // Prints MyClass`1[String]
        Console.WriteLine(typeof(AnotherClass).BaseType);
    }
}

Please post the code that's failing.

MyClass是否真的是一个界面?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM