What's a quick way to check if a reference is a specific generic type?

If I have a method with a parameter that has an interface, then what's the fasts way to see if the interface reference has a certain generic type?

Specifically, if I have:

interface IVehicle{}

class Car<T> : IVehicle {}

CheckType(IVehicle param)
{
    // How do I check that param is Car<int>?
}

      

I will also have to quit after checking. So if there is a way to kill 2 birds with one stone on this one, let me know.

0


source to share


6 answers


To check if a parameter is Car<int>

, you can use "is" and "as" as usual:



CheckType(IVehicle param)
{
    Car<int> car = param as Car<int>;
    if (car != null)
    {
         ...
    }
}

      

+10


source


Or you can just do:



if(param is Car<int>)
{
    // Hey, I'm a Car<int>!
}

      

+3


source


Why not make it common?

interface IVehicle{}

class Car<T> : IVehicle {

    public static bool CheckType(IVehicle param)
    {
        return param is Car<T>;
    }
}

      

...

Car<string> c1 = new Car<string>();
Car<int> c2 = new Car<int>();
Console.WriteLine(Car<int>.CheckType(c1));
Console.WriteLine(Car<int>.CheckType(c2));

      

+1


source


The code is very different depending on whether you want to know if the link is based on a generic prototype or a specialized one.

Specialized easy, you can just use is

:

CheckType(IVehicle param)
{
   var isofYourType = param is Car<int>;
   ...
}

      

or safe casting as shown above:

CheckType(IVehicle param)
{
   var value = param as Car<int>;
   if(value != null)    
       ...
}

      

If you need to know if yur var is just some specialization Car<T>

, things get really ugly . And the last thing to worry about is speed in this case, because that would be even uglier than g's code:

class Car<T>
{ }

interface IVehicle { }

class YourCar : Car<int>, IVehicle
{ }

static bool IsOfType(IVehicle param)
{
    Type typeRef = param.GetType();
    while (typeRef != null)
    {
        if (typeRef.IsGenericType &&
            typeRef.GetGenericTypeDefinition() == typeof(Car<>))
        {
            return true;
        }
        typeRef = typeRef.BaseType;
    }
    return false;
}

static void Main(string[] args)
{
    IVehicle test = new YourCar();
    bool x = IsOfType(test);
}

      

+1


source


I often find that if my code requires me to write checks for certain types, I'm probably doing something wrong. You haven't given us enough context to give us advice on this.

Does this suit your needs?

Car<int> carOfInt = param as Car<int>;
if (carOfInt != null)
{
    // .. yes, it a Car<int>
}

      

0


source


Use the how operator to do it all in one shot. "as" will return either an object of the type you want, or null if what you are checking does not match. This will only work for reference types.

interface IVehicle { }
class Car<T> : IVehicle 
{
    static Car<int> CheckType(IVehicle v)
    {
        return v as Car<int>;
    }
}

      

The "is" operator will allow you to check if it is an v

instance Car<int>

.

0


source







All Articles