返回 ObjectQuery使用反射从 objectQuery

发布于 2024-12-17 15:49:33 字数 893 浏览 0 评论 0原文

我有一种方法,该方法在此 ObjectQuery 对象中返回 ObjectQuery 的对象,对象的类型是 ObjectQuery,现在我想在此对象中包含表 使用反射,我调用了方法“包括使用反射”,但我收到错误,有人可以告诉我错误吗?这是示例代码。

ObjectQuery objTest = LoadEntitiy(entites,entityClassType);


public ObjectQuery LoadEntitiy(ClientEntities entities, Type entityClasstype)
        {
            PropertyInfo pi = entities.GetType().GetProperties().First(item => item.Name == entityClasstype.Name.ToString());
            Object obj = pi.GetValue(entities, null);
            Type objContext = obj.GetType();
            return (ObjectQuery)obj;
        }

现在我调用使用此处的反射包含它的方法

Type lstType = typeof(ObjectQuery<>); 
Type constructedType = lstType.MakeGenericType(typeof(ObjectQuery<>)); 
MethodInfo addListItemMethod = constructedType.GetMethod("Include"); 
addListItemMethod.Invoke(objTest, new object[] {"tablename" });

I have one method which is returning the object of ObjectQuery in this ObjectQuery object the type of object is ObjectQuery, now i want to include table in this object
using reflection, i called the method Include using reflection for this but i m getting the error can someone please tell me the error. here is the sample code.

ObjectQuery objTest = LoadEntitiy(entites,entityClassType);


public ObjectQuery LoadEntitiy(ClientEntities entities, Type entityClasstype)
        {
            PropertyInfo pi = entities.GetType().GetProperties().First(item => item.Name == entityClasstype.Name.ToString());
            Object obj = pi.GetValue(entities, null);
            Type objContext = obj.GetType();
            return (ObjectQuery)obj;
        }

now i m calling the method for including it using the reflection that is here

Type lstType = typeof(ObjectQuery<>); 
Type constructedType = lstType.MakeGenericType(typeof(ObjectQuery<>)); 
MethodInfo addListItemMethod = constructedType.GetMethod("Include"); 
addListItemMethod.Invoke(objTest, new object[] {"tablename" });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开浅夏 2024-12-24 15:49:33

您似乎想定义一个约定来始终“包含”一组特定的数据。

这种约定通常称为急切加载,并且还有诸如延迟加载之类的替代方案。

EF 4.1 或更高版本已包含为您执行此操作的功能,请参阅 http://msdn.microsoft.com/en-us/library/gg715120(v=vs.103).aspx

It seems that you want to define a convention to always "Include" a certain set of data.

This convention is normally known as eager loading and there are alternatives like lazy loading.

EF 4.1 or greater already includes functionality to do this for you, see http://msdn.microsoft.com/en-us/library/gg715120(v=vs.103).aspx

北方的韩爷 2024-12-24 15:49:33
Type lstType = typeof(ObjectQuery<>); 
Type constructedType = lstType.MakeGenericType(typeof(T(which u want to send as parameter))); 
MethodInfo addListItemMethod = constructedType.GetMethod("Include"); 
object objtest = addListItemMethod.Invoke(objTest, new object[] {"tblname" });

现在 objtest 包含您想要包含的所有表名称。

Type lstType = typeof(ObjectQuery<>); 
Type constructedType = lstType.MakeGenericType(typeof(T(which u want to send as parameter))); 
MethodInfo addListItemMethod = constructedType.GetMethod("Include"); 
object objtest = addListItemMethod.Invoke(objTest, new object[] {"tblname" });

Now the objtest contains the all the table names which you want to include.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文