tags:

views:

220

answers:

6
+2  Q: 

Generics c#.net

In the below code "where T : WsgTypes.RouteRestriction", can I add multiple classes so that T can be of only those few classes types which I am interested of

    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();
    }
+1  A: 

I'm afraid you can't. The usual way is to provide a common interface that all classes you're interested in implement. The problem is that, inside the generic body, the compiler expects a generic type parameter to be unambigous.

Well, or you could take an object as parameter and cast it at your will. But... no. Don't.

Note that instead of typeof, you could also use the is and as operators.

OregonGhost
+1  A: 

See this article...

http://msdn.microsoft.com/en-us/library/d5x73970.aspx

... for more information on constaints. You can add multiple constraints, and you can constrain by some interface or by some base class, but not by a list of arbitrary classes.

Here's an example of multiple constraints (from the above):

class Base { }
class Test<T, U>
    where U : struct
    where T : Base, new() { }
Martin Peck
That article explains how to use multiple constraints for the same type - i.e. the generic parameter must implement ALL of them.
OregonGhost
Your sample explains different constraints for different type arguments - that's probably not what Miral wants.
OregonGhost
Yep - misread the question. Editing now...
Martin Peck
A: 

For inheritance you can have a single class with multiple interfaces.

public static T GetDetails<T>(string code) where T : WsgTypes.RouteRestriction, , IComparable
    {
    }

Instead you can have an interface and have multiple classes implementing it.

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;
        }
Rashmi Pandit
A: 

Did you try separating them like this:

public static T GetDetails<T>(string code) where T : WsgTypes.RouteRestriction, NextClass, AnotherClass, AndSoOn
{
...
}
kyrisu
this is not possible
Miral
+4  A: 

No, generic type constraints can only specify a single base-class.

You can specify multiple interfaces, but this is "all of", not "any of".

What you ask is possible with overloading, but not with generics.

Marc Gravell
+5  A: 

It seems to me that this isn't a proper use of generics. It would be better if TicketType and RouteRestriction implemented some IDetailed.

bruno conde
Yep, that's a detailed version of what I described in general :)
OregonGhost