I want to get context form DbSet with below code, because Reflection is heavy I want to rewrite blow code with another code with better performance
Can anyone help me for a solution with better performance or rewriting code with FastMember library ?
public static DbContext GetContext<TEntity>(this DbSet<TEntity> dbSet)
where TEntity : class
{
object internalSet =
dbSet
.GetType()
.GetField("_internalSet", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(dbSet);
object internalContext = internalSet
.GetType()
.BaseType
.GetField("_internalContext", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(internalSet);
return (DbContext)internalContext
.GetType()
.GetProperty("Owner", BindingFlags.Instance | BindingFlags.Public)
.GetValue(internalContext, null);
}
Thanks
Aucun commentaire:
Enregistrer un commentaire