泛型c#.net

Mir*_*ral 3 c# generics

在下面的代码"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();
    }
Run Code Online (Sandbox Code Playgroud)

bru*_*nde 5

在我看来,这不是对泛型的正确使用.如果TicketTypeRouteRestriction实施一些会更好IDetailed.