背后的IQueryable访问的DataContext [英] Access DataContext behind IQueryable

查看:169
本文介绍了背后的IQueryable访问的DataContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能访问一个IQueryable背后的DataContext对象?

Is it possible to access the DataContext object behind an IQueryable?

如果是这样,怎么样?

推荐答案

的DataContext是针对LINQ到SQL,那么想必你在谈论的LINQ to SQL查询?如果是这样,有没有安全的方式来做到这一点 - 你不得不求助于黑客攻击,例如使用反射来获取底层DataQuery对象的私有上下文字段:

DataContext is specific to LINQ to SQL, so presumably you're talking about LINQ to SQL queries? If so, there's no safe way to do this - you have to resort to a hack such as using reflection to retrieve the private "context" field of the underlying DataQuery object:

static DataContext GetContext (IQueryable q)
{
  if (!q.GetType().FullName.StartsWith ("System.Data.Linq.DataQuery`1")) return null;
  var field = q.GetType().GetField ("context", BindingFlags.NonPublic | BindingFlags.Instance);
  if (field == null) return null;
  return field.GetValue (q) as DataContext;
}

这篇关于背后的IQueryable访问的DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆