我知道這方面有幾個問題,但我似乎無法得到它的工作。測試類是否繼承了通用接口
我有這樣的課;
public class TopLocation<T> : ILocation
{
public string name { get; set; }
public string address { get; set; }
}
當我創建我指定它是一個IRestaurant
或IClub
類。到目前爲止沒有問題。
但是,如何測試語句中的IClub
或IRestaurant
?
失敗;
if (item.trendItem is ILocation<ITopRestaurant>)
,並返回null
Type myInterfaceType = item.trendItem.GetType().GetInterface(
typeof(ITopRestaurant).Name);
我想這在if
聲明的原因是因爲它在MVC應用程序坐在一個ASCX頁面中,我試圖呈現部分正確視圖。
編輯
響應註釋;
所有的public interface ITopClub{}
public interface ITopRestaurant { }
public interface ILocation{}
有一個通用的`ILocation`以及?什麼是層次結構?什麼是「ITopRestaurant」? – Jon 2011-12-16 02:34:08
ITopRestaunt只是一個空接口,用於識別此項目的ILocation類型 – griegs 2011-12-16 02:35:12
可能的重複http://stackoverflow.com/questions/503263/how-to-determine-if-a-type-implements-a-specific -generic-interface-type – HackedByChinese 2011-12-16 02:36:11