在下面的代码“where T : WsgTypes.RouteRestriction”中,我可以添加多个类,以便 T 只能是我感兴趣的那几个类类型吗
public static T GetDetails<T>(string code) where T : WsgTypes.RouteRestriction
{
T details;
if (typeof(T) == typeof(WsgTypes.TicketType))
{
details = TicketTypeDetail.GetDetails(code) as T;
}
else if (typeof(T) == typeof(WsgTypes.RouteRestriction))
{
details = RouteRestrictionDetail.GetDetails(code) as T;
}
else
{
throw new NotSupportedException("");
}
return details;
throw new NotImplementedException();
}
最佳答案
在我看来,这不是对泛型的正确使用。如果 TicketType
和 RouteRestriction
实现一些 IDetailed
会更好。
关于c# - 泛型 c#.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/881922/