SilverDeveloper
SilverDeveloper

Reputation: 187

Get type of generic list

In code listed bellow I need determine type of TSource. Parsed parameter can be, for example IList, or only Car etc. In method Model.Map<> I need parse type of generic type. Everything works fine when parsed parameter is single object (Car, Boat...). Problem is when collection is parsed. So, I need to cover case when parameter is collection.

public class Convert<TSource, TDestination>
{
    public static TDestination ToModel(TSource source)
    {
        Model.Map<TSource, TDestination>();
    }
}

Upvotes: 0

Views: 1223

Answers (2)

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You can try with - based on GetGenericArguments

var type = source.GetType().GetGenericArguments()[0];

Link : http://msdn.microsoft.com/fr-fr/library/system.type.getgenericarguments.aspx

Upvotes: 4

Andrei R&#238;nea
Andrei R&#238;nea

Reputation: 20780

Try System.Type.GetGenericArguments

Upvotes: 0

Related Questions