繁体   English   中英

为什么我不能将IEnumerable <T>列表转换为BindingList <t>?

[英]Why can i not cast an IEnumerable<T> list to a BindingList<t>?

是否可以将IEnumerable列表转换为BindingList集合?

IEnumerable列表是类型化对象的列表,例如:

IEnumerable<AccountInfo> accounts = bll.GetAccounts(u.UserName, u.Password);

而我的PagingList只是扩展了BindingList:

public class PagingList<T> 
{
    public BindingList<T> Collection { get; set; }
    public int Count { get; set; }

    public PagingList()
    {
        Collection = new BindingList<T>();
        Count = 0;
    }
}

我只是想将我的IEnumerable列表传递给一个使用我的PagingControl呈现列表的方法:

 protected void RenderListingsRows(PagingList<AccountInfo> list)
   {
     foreach (var item in list)
     {
       //render stuff
     }
   }

但似乎我不能在两者之间施展,任何人都可以指出我错过了什么?!

非常感谢

BindingList<T>实现IEnumerable<T> ,但并非所有IEnumerable<T>都是绑定列表(事实上,大多数都不是)。

您应该能够创建一个新的BindingList并在可枚举实例中添加项目。

只是要指出,你的PagingList没有扩展BindingList,它通过组合使用它。

我遇到了这个寻找类似的答案。 这里的答案似乎都没有为你的问题提供一个明确的解决方案,尽管他们在提出问题时提到了有价值的观点。 我以为我会为路过的人添加一个。

因此,鉴于提供的信息,简单的答案是否定的,但是如果不重构您的类,您需要的简单解决方案是:

IEnumerable<AccountInfo> accounts= bll.GetAccounts(u.UserName, u.Password);
myPagingList.Collection = new BindingList<Foo>(myfoos.ToList());

因此,您必须将您的AccountInfo项物理添加到BindingList实例属性'Collection'。

您将PagingList传递给RenderListingsRows,它不实现IEnumerable。

通常,要使PagingList成为BindingList的扩展,它必须实现BindingList实现的所有接口。 但目前它没有实现任何一个。

您应该从BindingList继承PagingList,或者实现所有这些接口,即使只是通过调用Collection对象的方法也是如此。

或者,你可以简单地写一下(list.Collection中的var项)

如果您的帐户集合实现IList <AccountInfo>,您应该能够这样做:

PagedList<AccountInfo> paged = new PagedList<AccountInfo>();
paged.Collection = new BindingList<AccountInfo>((IList<AccountInfo>)accounts);

将绑定列表发送到RenderListingsRows而不是分页列表。 PagingList不会扩展BindingList,而是使用composision。 因此这个问题。

以下示例。

 public class PagingList<T>
        {
            public BindingList<T> Collection { get; set; }
            public int Count { get; set; }

            public PagingList()
            {
                Collection = new BindingList<T>();
                Count = 0;
            }

        }

        public void CallRenderListingsRows()
        {
            var pagingList = new PagingList<PostcodeDetail>();

            RenderListingsRows(pagingList.Collection);
        }

        protected void RenderListingsRows(BindingList<PostcodeDetail> list)
        {
            foreach (var item in list)
            {
                //render stuff
            }
        }

为什么我不能从列表中投射<myclass>列出<object> ?<div id="text_translate"><p> 我有一个对象列表,它们属于我的QuoteHeader类型,我想将此列表作为对象列表传递给能够接受List&lt;object&gt;的方法。</p><p> 我的代码行显示...</p><pre> Tools.MyMethod((List&lt;object&gt;)MyListOfQuoteHeaders);</pre><p> 但是我在设计时收到以下错误...</p><pre> Cannot convert type 'System.Collections.Generic.List&lt;MyNameSpace.QuoteHeader&gt;' to 'System.Collections.Generic.List&lt;object&gt;'</pre><p> 我需要对我的 class 做任何事情来允许这样做吗? 我认为所有类都继承自 object 所以我不明白为什么这不起作用?</p></div></object></myclass>

[英]Why can't I cast from a List<MyClass> to List<object>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM