如何构建ObservableCollection <t>,其中T将ISomething实现为IList <isomething>

时间:2016-02-13 20:58:19

标签: c#

convert List<List<object>> to IList<IList<object>>

Does Enumerable.Cast<T> Copy Objects?

https://msdn.microsoft.com/en-us/library/ms668604(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ms132397(v=vs.110).aspx

我的班级Person实现了我的界面,ISequenced。

我有一个ObservableCollection&lt; Person&gt;我需要传递给一个方法,该方法将IList&lt; ISequenced&gt;类型的对象作为参数。 我的问题是我如何传递ObservableCollection - 没有复制 - 这样被调用的函数可以修改它。

请注意这个问题的答案:Is there a way to convert an observable collection to regular collection? 演示.ToList()创建一个新对象。

public interface ISequenced
{
    int Sequence { get; set; }
}


public class Person : ISequenced
{
    public int Sequence {get; set;}
    //...
}

public class MyCode
{
    public ObservableCollection<Person> Entities {get; set;}
    public Person CurrentItem {get;set;}


    private void MoveDown()
    {
        IEnumerable<ISequenced> seq = Entities.Cast<ISequenced>(); // works but we need IList not IEnumerable
        IList<Person> p = (IList<Person>)Entities;                  //  works but we need IList ISequenced
        IList<ISequenced> pp = (IList<ISequenced>)p;                // throws

        // Need to pass a reference to Entities.... throws unable to cast here
        Sequencer.SequenceDown((IList<ISequenced>)Entities, CurrentItem as ISequenced);

    }
}

public static class Sequencer
{
    public static void SequenceDown(IList<ISequenced> items, ISequenced currentItem)
    {
        // swap the positions of two elements in the collection here
    }
}

0 个答案:

没有答案