dimanche 11 décembre 2016

Get Class Reference from Class Type or String

So I've been looking around at a ton of similar questions but none of them seem to be attempting the same thing I am. I need a reference to a class, not a class instance.

I am trying to dynamically make a class reference for a generic type function. My function is as follows:

private void CleanupTable<T, U>(DbSet<T> dbSet, CleanupModel.DeftlyTables table, DbSet<U> lastDbSet, dynamic removedRec) where T : class where U : class
{
    ParameterExpression tpe = Expression.Parameter(typeof(T));
    Expression idProp = Expression.Property(tpe, typeof(T).GetProperty(GetIdProperty(lastDbSet)));
    Expression constIdProp = Expression.Constant(removedRec.GetType().GetProperty(GetIdProperty(lastDbSet)).GetValue(removedRec, null), typeof(int));
    Expression completeExpression = Expression.Equal(idProp, constIdProp);

    Expression<Func<T, bool>> expression = Expression.Lambda<Func<T, bool>>(completeExpression, tpe);
    List<T> removedRecs = dbSet.Where(expression).ToList();

    removedRecs.ForEach(rec =>
    {
        DbSet nextSet = GetNextSet(dbSet);

        //Here is where I'm trying to create a reference using nextSet
        CleanupTable</*nextSetType reference*/, T>(nextSet, GetNextTable(dbSet), dbSet, rec);

        dbSet.Remove(rec);
        reportHelper.ReportSuccess(table, ReportHelper.ReportReasons.Linked, rec);
    });
}

I have tried using things like GetType() but the generic does not accept a Type. Is what I'm trying to do even possible?





Aucun commentaire:

Enregistrer un commentaire