泛型c#.net
发布时间:2020-12-16 10:15:34 所属栏目:百科 来源:网络整理
导读:在下面的代码“where T:WsgTypes.RouteRestriction”中,我可以添加多个类,以便T只能是我感兴趣的那几个类类型 public static T GetDetailsT(string code) where T : WsgTypes.RouteRestriction { T details; if (typeof(T) == typeof(WsgTypes.TicketType))
|
在下面的代码“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();
}
解决方法
对于继承,您可以拥有一个具有多个接口的类.
public static T GetDetails<T>(string code) where T : WsgTypes.RouteRestriction,IComparable
{
}
相反,您可以拥有一个接口并具有多个实现它的类. public interface IInterface
{}
public class Class1: IInterface
{}
public class Class2: IInterface
{}
public static T GetDetails<T>(string code) where T:IInterface
{
T instance;
// ...
return instance;
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
