如何检查类型参数?

时间:2013-07-31 16:12:11

标签: c#

我有课

class ManagerCar : IBlalba
{
   public void Render(IViewTemplate template)
   {
   }
}

我知道参数模板是什么CarViewTemplate我希望检查类型并在IViewTemplate != CarViewTemplate

时抛出异常

我可以查看if(template is typeof(CarViewTemplate))

但我有很多Managers

我希望在编译时检查此类型,并使用[IsType(typeof(CarViewTemplate))]

等帮助属性

有可能吗?

更新 我有Render(IViewTemplate template)

的界面

1 个答案:

答案 0 :(得分:2)

这种类型的检查已经存在于编译器中,无需尝试帮助它。

只需将您的方法签名更改为:

public void RenderCar(CarViewTemplate template)