0

I have a bit complicated issue to solve.

I have a list of objects though later the list will be filled with two different type of instances.

First type is MyFirstType<T1, T2> and second type is MySecondType<T>

Now I need to run though the list of objects and ask which one of the two is each item. Then I need to do some custom logic on the item.

For example:

foreach(object obj in list)
{
   if(obj is MyFirstType<T1, T2>)
   {
     // do something
   }
   else if(obj is MySecondType<...>)
   {
   }
}

The problem is T or T1 and T2 could be any types so how do I write such an if - Is Keyword statement that only comparies if MyFirstType but not the generics inside? if(obj is MyFirstType<T1, T2>) does not work since it needs concrete types for T1 and T2.

Like mentioned just need comparison without T1 and T2. Any ideas how to solve this?

dev hedgehog
  • 8,698
  • 3
  • 28
  • 55
  • http://stackoverflow.com/q/457676 – Robert Harvey Apr 21 '14 at 19:44
  • 2
    This kind of type checking is almost always a product of taking the wrong approach to solve a problem; can you be a bit less abstract and detail what the actual task is and how you're trying to solve it? – Ant P Apr 21 '14 at 19:44
  • @AntP Thanks but no thanks The question was not shall I change my design or not. It is clear enough what the problem is. If you cannot solve this please do not start discussion how could/should it have been done right. – dev hedgehog Apr 21 '14 at 19:47
  • 1
    @devhedgehog That is a pretty poor attitude but, sure, have it your way. I'm not going to waste my time helping you implement a sub-par solution, though I'm sure others will. – Ant P Apr 21 '14 at 19:48
  • Yea right mr ant, please move on – dev hedgehog Apr 21 '14 at 19:49

3 Answers3

2

You can use IsGenericType property of Type class to check if obj.GetType() is generic, and then use GetGenericTypeDefinition to get generic type definition, which can be compared to typeof(MyFirstType<,>) and typeof(MySecondType<>):

foreach(object obj in list)
{
    if(obj.GetType().IsGenericType)
    {
        if(obj.GetType().GetGenericTypeDefinition() == typeof(MyFirstType<,>))
        {
          // do something
        }
        else if(obj.GetGenericTypeDefinition() == typeof(MySecondType<>))
        {
        }
    }
}
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
2
  1. The cheap solution: create a flag interface which you derive in your types. This is just a helper to determine the type.
  2. Use reflection and check the type definition and check the name of the type.
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
2

Why don't you use polymorphism? Have the two types implement an interface with a common method. Then in the implementation of each type write the code for the method you need.

public interface ICommon
{
    void DoThis();
}

For MyFirstType and MySecondType you will have:

public class MyFirstType<T> : ICommon
{
    public void DoThis()
    {
        //DoThis
    }
}

public class MySecondType<T1,T2> : ICommon
{
    public void DoThis()
    {
        //DoThis
    }
}

Then you could write the following foreach loop:

foreach(object obj in list)
{
    obj.DoThis();
}
Ben
  • 538
  • 1
  • 9
  • 24
  • 1
    +1 for a good answer, though likely a wasted effort given comments. – Ant P Apr 21 '14 at 20:03
  • I cannot change the types. They are third party. They are sealed. Thanks Ben anyway. I appreciate your suggestion. @AntP This is not a waisted efford however your comments are waisted efford. Havent you said you moved on? – dev hedgehog Apr 21 '14 at 21:08