C#反射绑定结果:将Linq2Sql绑定到列表

4

我是一个新手,正在使用反射技术,在搜索了三天后始终没有结果。希望能得到一些帮助。

我的目标是将 Result 绑定到列表中。

我有一个类:

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;
    } 
}

我将这个绑定到一个下拉框:

StatuscomboBoxProperties.DataSource = getDropdownList("Get_SupplierList");

在toReturn中的"result"会给出以下错误:
could not find an implementation of the query pattern for source type "system.Reflection.MethodInfo' "Select" not found

我的问题是:如果你将鼠标悬停在结果上,会出现一个“结果视图”,我可以看到从方法返回的数据,但是我该如何将我的列表与该数据绑定?

1
“Invoke” 返回一个对象。因此,结果是一个对象。您应该知道一个常见的基类型(或接口),并将“method.Invoke”的结果转换为它。我还认为您应该找到另一种方法来完成它,因为在您的情况下使用反射是不合适的。 - Ahmed KRAIEM
1
不,你不想在这里使用反射。如果你愿意接受替代方案,请告诉我,我会使用委托来制作一个演示。 - Mister Epic
任何替代方案都可以。重要的是我将方法作为字符串传递,因为我已经构建了一个框架,从独立可执行文件中控制我的网格(devexpress)的外观和感觉。这是我绑定下拉列表到必要的网格组合框的最后一步。 - Hennie van den Berg
忘记在我的评论中@ChrisHardie,谢谢。 - Hennie van den Berg
1个回答

0

我改变了我的方法,我的应用程序运行得很好。欢迎任何对我的新方法的评论或建议。

我保留了我的类:

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;
    } 
}

我将它绑定到一个下拉框:

StatuscomboBoxProperties.DataSource = getDropdownList("Get_SupplierList");

"Get_SupplierList" 是由一个组合框传入的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接