我使用实体框架。我创建实体模型。我想在运行时从 DbContext 获取所有类型的实例。
public class MyClass
{
WdmEntities _context = = new WdmEntities();
ObjectContext objContext = ((IObjectContextAdapter)_context).ObjectContext;
EntityContainer container = objContext.MetadataWorkspace.GetEntityContainer(objContext.DefaultContainerName, DataSpace.CSpace);
//even if c=>c.FullName
List<string> nameTypes = container.BaseEntitySets.OfType<EntitySet>().Select(c=>c.Name).ToList();
List<Type> types = new List<Type>();
foreach(var name in nameTypes)
{
//.GetType return null
types.Add(Type.GetType(name));
}
}