2012-07-26 12 views
5

Tło:Jak mogę uprościć wywołanie metody rozszerzenia Linq za pomocą odbicia?

Mam usługi internetowej, że liczba wierszy tabeli (nazwa tabeli dostarczany jako parametru) z identyfikatorów większej od pewnej Id (również dostarczany jako parametru). Zakładamy, że identyfikatory są sekwencyjne.

używam LINQ do SQL do bazy danych interakcji więc chcę wrócić nowe wiersze jak:

List<WhateverObject> 

Bo wiemy tylko nazwę tabeli w czasie wykonywania, nie mogę użyć LINQ w normalny sposób co sprawiło, że sprawy stały się bardziej złożone.

Pytanie:

Kod jest poniżej (i to działa). Jak mogę to uprościć? Wydaje się to zbyt skomplikowane.

private object GetUpdateList(string tableName, int Id, DataClassesDataContext db) 
{ 
    PropertyInfo pi = db.GetType().GetProperty(tableName); 

    var table = pi.GetValue(db, null); 

    // Get type of object within the table. 
    Type genericType = table.GetType().GetGenericArguments()[0]; 

    // The Where method lives on the Enumerable type in System.Linq 
    var whereMethods = typeof(System.Linq.Enumerable) 
     .GetMethods(BindingFlags.Static | BindingFlags.Public) 
     .Where(mi => mi.Name == "Where"); 

    // There are actually 2 where methods - we want the one with 2 parameters 
    MethodInfo whereMethod = null; 
    foreach (var methodInfo in whereMethods) 
    { 
     var paramType = methodInfo.GetParameters()[1].ParameterType; 
     if (paramType.GetGenericArguments().Count() == 2) 
     { 
      // we are looking for Func<TSource, bool>, the other has 3 
      whereMethod = methodInfo; 
      break; 
     } 
    } 

    Func<object, bool> IdEquals = BuildEqFuncFor("Id", Id); 

    whereMethod = whereMethod.MakeGenericMethod(genericType); 
    var result = whereMethod.Invoke(table, new object[] { table, IdEquals }); 

    MethodInfo toListMethod = typeof(System.Linq.Enumerable).GetMethod("ToList").MakeGenericMethod(genericType); 
    return toListMethod.Invoke(result, new object[] { result }); 
} 

// Build lambda expression for use in Linq 
private static Func<object, bool> BuildEqFuncFor(string prop, object val) 
{ 
    // We know we are comparing integers here so cast them. 
    // There is probably a more general solution. 
    return t => (int)t.GetType().InvokeMember(prop, BindingFlags.GetProperty, null, t, null) > (int)val; 
} 

wymyślić tego rozwiązania musiałem odwołać się następujące pytania:

+0

Nie można zrzucić pytanie, ale czy jesteś w sytuacji, w której niemożliwe jest refactor? Wydaje się, że dużo pracy za coś tak prostego. Czy musi być tak ogólny? –

+0

Co byś zasugerował? –

+0

Po prostu mocno napisać? Przekaż nazwę tabeli i identyfikator. Wybierz go z bazy danych i zwróć żądane wiersze. Zwróć wartość 'IEnumerable ' i nazwij ją raz dziennie. –

Odpowiedz

4

Spróbuj czegoś takiego:

private IList GetUpdateList(string tableName, int id, DataClassesDataContext db) 
{ 
    System.Reflection.PropertyInfo pi = db.GetType().GetProperty(tableName); 

    var table = pi.GetValue(db, null); 

    // Get type of object within the table. 
    Type genericType = table.GetType().GetGenericArguments()[0]; 

    var param = Expression.Parameter(genericType, "x"); 
    var predicateExpr = Expression.Lambda(
     Expression.GreaterThan(
      Expression.Property(param, "Id"), 
      Expression.Constant(id)), 
     param); 

    return this 
     .GetType() 
     .GetMethod("GetUpdateListGeneric") 
     .MakeGenericMethod(genericType) 
     .Invoke(this, new[] { table, predicateExpr }) as IList; 
} 

private IList<T> GetUpdateListGeneric<T>(
    Table<T> table, 
    Expression<Func<T, bool>> predicate) where T : class 
{ 
    return table.Where(predicate).ToList(); 
} 
+0

Awesome! Musiałem tylko edytować kilka rzeczy (typ zwracany) i najpierw przeciągnąć Expression.Parameter do zmiennej i to działa. –

+0

Cieszę się, że pomógł, i dzięki za poprawki. – Jacob

Powiązane problemy