我有一个方法是在这个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" });发布于 2011-11-22 04:00:38
您似乎希望定义一个约定,使其始终“包含”特定的数据集。
这种约定通常被称为急切加载,还有一些替代方案,比如延迟加载。
EF 4.1或更高版本已经包含了为您完成此操作的功能,请参阅http://msdn.microsoft.com/en-us/library/gg715120(v=vs.103).aspx
发布于 2011-11-22 15:31:03
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包含了您想要包含的所有表名。
https://stackoverflow.com/questions/8208055
复制相似问题