views:

181

answers:

3

Is it possible to do something like this in C#:

public void DoSomething<T>(T t)  
{
    if (T is MyClass)
    {
        MyClass mc = (MyClass)t 
        ...
    }
    else if (T is List<MyClass>)
    {
        List<MyClass> lmc = (List<MyClass>)t
        ...
    }
}
+1  A: 

Yes:

if (typeof(T) == typeof(MyClass))
{
    MyClass mc = (MyClass)(object) t;
}
else if (typeof(T) == typeof(List<MyClass>))
{
    List<MyClass> lmc = (List<MyClass>)(object) t;
}

It's slightly odd that you need to go via a cast to object, but that's just the way that generics work - there aren't as many conversions from a generic type as you might expect.

Of course another alternative is to use the normal execution time check:

MyClass mc = t as MyClass;
if (mc != null)
{
    // ...
}
else
{
    List<MyClass> lmc = t as List<MyClass>;
    if (lmc != null)
    {
        // ...
    }
}

That will behave differently to the first code block if t is null, of course.

I would try to avoid this kind of code where possible, however - it can be necessary sometimes, but the idea of generic methods is to be able to write generic code which works the same way for any type.

Jon Skeet
I actually have a little more complicated issue. What if MyClass derives from MyBaseClass and there are many more MyClasses all of which deriving from MyBaseClass?
synergetic
@synergetic: You've described the type hierarchy, but not what you want to do with it. You can use reflection (e.g. `typeof(T).BaseType` or `typeof(T).IsAssignableFrom(...)` to explore the type hierarchy if that's useful. I'd still try to avoid it if possible though :)
Jon Skeet
Thank you Jon Skeet. Your answers were very helpful.
synergetic
A: 

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

ratty
A: 

I believe there's something wrong in your design. You want to compare between types in an already generic method. Generics are meant to deal with type-variable situation. I recommend to do it this way..

//Generic Overload 1
public void DoSomething<T>(T t)
    where T : MyClass
{
    ...
}

//Generic Overload 2
public void DoSomething<T>(T t)
    where T : List<MyClass>
{
    ...
}
this. __curious_geek