C#反射绑定结果Linq2Sql到列表
我是一个新使用反射的人,经过三天的搜索,我没有得到一个结果。我希望能得到一些帮助 我试图做的是将结果绑定到一个列表 我有一门课:C#反射绑定结果Linq2Sql到列表,c#,linq-to-sql,reflection,binding,C#,Linq To Sql,Reflection,Binding,我是一个新使用反射的人,经过三天的搜索,我没有得到一个结果。我希望能得到一些帮助 我试图做的是将结果绑定到一个列表 我有一门课: public class DropdownList { public string ID { get; set; } public string Description { get; set; } } 我有一个功能: public static List<DropdownList> getDropdownList(string Metho
public class DropdownList
{
public string ID { get; set; }
public string Description { get; set; }
}
我有一个功能:
public static List<DropdownList> getDropdownList(string Method)
{
using (var Context = new WebDataContext())
{
var method = Context.GetType().GetMethod(Method);
if (method == null) throw new InvalidOperationException("Defined DataContext does not have method" + Method);
var result = method.Invoke(Context,null);
var toReturn = (from x in result select new DropdownList { ID = x.???, Description = x.??? }).ToList();
return toReturn;
}
}
toReturn中的“结果”给出以下错误:
could not find an implementation of the query pattern for source type "system.Reflection.MethodInfo' "Select" not found
我的问题是:如果您将鼠标悬停在result上,则有一个result视图,我可以看到从该方法返回的数据,但我将如何绑定该数据的列表。我改变了方法,我的应用程序工作得很好。如果您对我的新方法有任何意见或建议,我们将不胜感激 我继续上课:
public class DropdownList
{
public int ID { get; set; }
public string Description { get; set; }
}
我将我的功能更改为:
public static List<DropdownList> getDropdownList(string Method)
{
using (var Context = new WebDataContext())
{
var toReturn = Context.ExecuteQuery<DropdownList>("EXEC " + Method).ToList();
return toReturn;
}
}
“Get\u SupplierList”由一个组合框传入。
Invoke
返回一个对象。所以结果是一个对象
。您应该知道一个公共基类型(或接口),并将方法的结果强制转换为Invoke。我还认为你应该找到另一种方法,因为在你的情况下使用反射是不合适的。不,你不想在这里使用反射。如果您愿意接受其他解决方案,请告诉我,我将使用代理制作一个演示。任何其他解决方案都可以。我必须将该方法作为字符串传递,因为我已经构建了一个框架,可以从一个独立的可执行文件控制网格(devexpress)的外观。这是我将下拉列表绑定到网格中需要的组合框的最后一步。忘记将您添加到我的评论@ChrisHardie谢谢
public static List<DropdownList> getDropdownList(string Method)
{
using (var Context = new WebDataContext())
{
var toReturn = Context.ExecuteQuery<DropdownList>("EXEC " + Method).ToList();
return toReturn;
}
}
StatuscomboBoxProperties.DataSource = getDropdownList("Get_SupplierList");