lundi 26 octobre 2015

Runtime-Type of T in IQueryable

Is there a possibility to get the T-Runtime type of an IQueryable? If it's a normal list, there is no problem, but if I check against an IQueryable, I always get a type of System.Object. My code is looking like this at the moment:

private static Type GetDtoTypeFromDataSource(ReportDataSource dataSourceFromDb)
{
    Type result = null;
    if (dataSourceFromDb.Data.GetType().IsGenericType)
    {
        Type[] genericArguments = dataSourceFromDb.Data.GetType().GetGenericArguments();
        result = genericArguments.First();

        if (result == typeof(Object))
        {
            var obj = dataSourceFromDb.Data.Cast<object>().ToList();

            var t2 = obj.First().GetType();
            var t = obj.GetType().GetGenericArguments();
            var t3 = obj.GetType();

            result = obj.GetType();
        }
    }
    else
    {
        result = dataSourceFromDb.Data.GetType().GetElementType();
    }

    return result;
}

I hoped, if I actually load the List via "ToList" I get the Data, but doesnt work as well.

var t2 = obj.First().GetType();

Would to the trick, but what if the List is empty? This doesn't seem like a good solution to me?





Aucun commentaire:

Enregistrer un commentaire