【问题标题】:C# Reflection binding result Linq2Sql to listC# 反射绑定结果 Linq2Sql 到列表
【发布时间】:2013-10-11 14:12:09
【问题描述】:

我是使用反射的新手,经过三天的搜索,我无法得到结果。我将不胜感激。

我想要做的是将 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 中的“结果”给出以下错误:

could not find an implementation of the query pattern for source type "system.Reflection.MethodInfo' "Select" not found

我的问题是:如果您将鼠标悬停在结果上,则有一个结果视图,我可以看到从该方法返回的数据,但是我将如何将我的列表绑定到该数据。

【问题讨论】:

  • Invoke 返回一个object。所以结果是object。您应该知道一个通​​用的基本类型(或接口)并将method.Invoke 的结果转换为它。我还认为您应该找到另一种方法,因为在您的情况下不适合使用反射。
  • 不,你不想在这里使用反射。如果您愿意使用其他解决方案,请告诉我,我将使用委托制作一个演示。
  • 任何替代解决方案都可以。将方法作为字符串传递很重要,因为我已经构建了一个框架,该框架可以从独立的可执行文件中控制网格 (devexpress) 的外观。这是我将下拉列表绑定到网格中需要的组合框的最后一步。
  • 忘记在我的评论中添加你@ChrisHardie 谢谢

标签: c# linq-to-sql reflection binding


【解决方案1】:

我改变了方法,我的应用程序运行良好。对我的新方法的任何评论或建议将不胜感激。

我继续上课:

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”由组合框传入。

【讨论】:

    猜你喜欢
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多