views:

119

answers:

1

How can i extract list of Types from ObjectContext?

e.g., i have object context contains entity named "Bank" and entity named "Company". I want to get the EntityObject type of them.

How can i do this?

+1  A: 

I assume you at run-time want to query the generated ObjectContext class to get a list of EntityObject classs. It then becomes an exercise in 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];

This code will find all public properties on the object context that has type System.Data.Objects.ObjectQuery<T> where T is a subclass of EntityObject.

Martin Liversage