如何从 ObjectContext
中提取 Types
列表?
例如,我的对象上下文包含名为“Bank”的实体和名为“Company”的实体。 我想获取它们的 EntityObject 类型。
我该怎么做?
最佳答案
我假设您在运行时想要查询生成的 ObjectContext
获取 EntityObject
列表的类类。然后它变成了反射(reflection)练习:
PropertyInfo[] propertyInfos = objectContext.GetType().GetProperties();
IEnumerable<Type> entityObjectTypes =
from propertyInfo in propertyInfos
let propertyType = propertyInfo.PropertyType
where propertyType.IsGenericType
&& propertyType.Namespace == "System.Data.Objects"
&& propertyType.Name == "ObjectQuery`1"
&& propertyType.GetGenericArguments()[0].IsSubclassOf(typeof(EntityObject))
select propertyType.GetGenericArguments()[0];
此代码将在类型为 System.Data.Objects.ObjectQuery<T>
的对象上下文中找到所有公共(public)属性其中 T
是 EntityObject
的子类.
关于c# - 从 EntityFramework ObjectContext 获取类型集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1184208/