C# 从类型本身获取类

C# 从类型本身获取类,c#,asp.net,entity-framework,linq,C#,Asp.net,Entity Framework,Linq,我试图用一种通用方法将所有dropdownlist绑定到SQL中相应的DB表(仅用于测试目的)。我使用的是实体框架。我几乎做到了,但我认为我被困在了一个不可能完成的任务上(至少没有什么可取之处),不管是哪一个,都要获取DbSet。代码如下: ... List<WebControl> myWebControl = new List<WebControl>(); GetWholeControls<WebControl>(Controls, ref myWebCo

我试图用一种通用方法将所有dropdownlist绑定到SQL中相应的DB表(仅用于测试目的)。我使用的是实体框架。我几乎做到了,但我认为我被困在了一个不可能完成的任务上(至少没有什么可取之处),不管是哪一个,都要获取DbSet。代码如下:

...
List<WebControl> myWebControl = new List<WebControl>();

GetWholeControls<WebControl>(Controls, ref myWebControl); //Get all the controls in the page

myDBentity = new TimeSheetDBEntity(); /My EF


foreach (WebControl childControl in myWebControl) //Loop all the controls
{
    if (childControl is DropDownList) //Get only ddl controls
    {

         List<Type> typelist = (List<Type>)from p in typeof(TimeSheetDBEntity).GetProperties()
                                                      where p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)
                                                      select p.Name; //get all DBSet properties of my EF

          foreach (Type currentType in typelist) //Loop the properties
          {
              if (childControl.ID == currentType.Name) //Compare with the ddl controls name (the first one is for example Product
              {

                 ((DropDownList)childControl).DataSource = myDBentity.Product.ToList(); //HOW TO GET PRODUCT BACK!!
                 ((DropDownList)childControl).DataTextField = "Name";
                 ((DropDownList)childControl).DataValueField = "Name";
                 ((DropDownList)childControl).DataBind();
               }              
            }
       }
}
....



public static void GetWholeControls<T>(ControlCollection pageControls, ref List<T> myWebControl)
    where T : Control
{
    foreach (Control control in pageControls)
    {
        if (control is T)
            myWebControl.Add((T)control);

         if (control.HasControls())
            GetWholeControls(control.Controls, ref myWebControl);
    }
}
。。。
List myWebControl=新建列表();
GetWholeControl(控件,参考myWebControl)//获取页面中的所有控件
myDBentity=new TimeSheetDBEntity()/我的EF
foreach(myWebControl中的webcontrolchildcontrol)//循环所有控件
{
if(childControl是DropDownList)//仅获取ddl控件
{
List typelist=(List)来自typeof(TimeSheetDBEntity.GetProperties()中的p
其中p.PropertyType.IsGenericType&&p.PropertyType.GetGenericTypeDefinition()==typeof(DbSet)
选择p.Name;//获取我的EF的所有DBSet属性
foreach(typelist中的Type currentType)//循环属性
{
if(childControl.ID==currentType.Name)//与ddl控件名称进行比较(第一个是例如Product
{
((DropDownList)childControl.DataSource=myDBentity.Product.ToList();//如何取回产品!!
((DropDownList)childControl.DataTextField=“Name”;
((DropDownList)childControl.DataValueField=“Name”;
((DropDownList)childControl.DataBind();
}              
}
}
}
....
公共静态void GetWholeControl(ControlCollection页控件,参考列表myWebControl)
其中T:控制
{
foreach(页面控件中的控件)
{
如果(控制为T)
添加((T)控件);
if(control.HasControls())
GetWholeControl(control.Controls,参考myWebControl);
}
}

我想得到'Product'类型,这样我就可以得到项目列表并将它们放入ddl中。当然,控件名和类类型名是相等的(“Product”),而textfield/valuefield总是“name”。我认为这无法以正常的方式实现,因为我无法从仅在运行时已知的类型创建编译时类型…可能使用反射或激活器。CreateInstance?..

实体框架上下文有一种按类型获取集合的方法,而不是直接访问DbSet属性

因此,您可以调用
myDBEntity.Set(currentType)AsQueryable().ToList();
,而不是
myDBEntity.Products.ToList();


你说的“找回类”是什么意思?类的新实例?同意。代码混乱,解释不好。我将编辑它,试图解释我自己。基本上,我只想在运行时获得符合上述条件的数据库集。谢谢你的回复。我尝试过这个,但设置(键入),只返回一个DbSet而不返回DbSet,因此我恐怕这里没有ToList()。我是否缺少额外的步骤?它确实返回一个类型DbSet,您可能需要放置.AsQueryable(),我将更新答案您的意思是?myDBentity.Set(currentType).AsQueryable().ToListAsync())。我想你也忘了一点。无论如何,我用另一个解决方案重新编写了我所有的代码,现在正在工作。谢谢你的努力。