C# 测试类是否继承泛型接口

C# 测试类是否继承泛型接口,c#,generics,C#,Generics,我知道在这方面已经有一些问题了,但我似乎无法让它发挥作用 我有一节这样的课 public class TopLocation<T> : ILocation { public string name { get; set; } public string address { get; set; } } 这个返回null Type myInterfaceType = item.trendItem.GetType().GetInterface

我知道在这方面已经有一些问题了,但我似乎无法让它发挥作用

我有一节这样的课

public class TopLocation<T> : ILocation
{
    public string name { get; set; }
    public string address { get; set; }
}
这个返回null

               Type myInterfaceType = item.trendItem.GetType().GetInterface(
                   typeof(ITopRestaurant).Name);
我之所以希望在
if
语句中使用它,是因为它位于MVC应用程序的ascx页面中,我正在尝试呈现正确的局部视图

编辑

对评论的回应

public interface ITopClub{}
public interface ITopRestaurant { }
public interface ILocation{}

首先,
ILocation
不是一个通用接口,因此尝试对
ILocation
进行任何测试都将失败。您的类是泛型类型

第二,您试图弄清楚作为泛型类型的泛型参数使用的类型是否是给定的接口。为此,需要获取该类型的泛型类型参数,然后对该类型执行检查:

var myInterfaceType = item.trendItem.GetType().GetGenericTypeArguments()[0];

if(myInterfaceType == typeof(ITopRestaurant))
{

}

您可以简单地执行以下操作:

if (item.trendItem is TopLocation<IRestaurant>) 
if(item.trendItem为TopLocation)

是否也有一个通用的
ILocation
?什么是等级制度?什么是
ITopRestaurant
?ITopRestaurant只是一个用于识别ILocation类型的空界面。此项可能与@HackedByChinese重复,看到了这一项,但看起来有点复杂,因为我认为这应该是一个相当直接的操作kwim?太棒了。谢谢@JustinNiessner。非常好。事实上,是的,这正是我想要的。谢谢
if (item.trendItem is TopLocation<IRestaurant>)