1
私は同様の質問トンで見てきましたが、それらのどれも私が同じことを試みているようです。クラスインスタンスではなく、クラスへの参照が必要です。クラスの型または文字列からクラスの参照を取得
ジェネリック型関数のクラス参照を動的に作成しようとしています。次のように私の機能は次のとおりです。ここで
private void CleanupTable<T, U>(DbSet<T> dbSet, CleanupModel.Tables 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);
});
}
はGetNextSet()
のコードです:
private DbSet GetNextSet(CleanupModel.Tables table)
{
switch (table)
{
case CleanupModel.Tables.Version: return context.Page;
//More cases
default: return null;
}
}
私はGetType()
のようなものを使用して試してみましたが、ジェネリックがType
を受け付けません。私がしようとしていることは可能なのでしょうか?
あなたは[MakeGenericMethod]使用することができます(https://でMSDN .microsoft.com/ja-us/library/system.reflection.methodinfo.makegenericmethod(v = vs.110).aspx)。最後の例を確認してください –
私はそれが私が必要なことをしているのか分かりません。あるいは、私はそれを十分に理解していないかもしれません。あなたは詳しく説明できますか? –
タイプ推論が機能しないのはなぜですか? 'GetNextSet'の署名は何ですか? 'nextSet'のタイプは何ですか? – InBetween