How to deserialize Enumerable.ToList <> () for list <>

I am trying to create an object that looks something like this:

  public class MyObject
  {
    private IList<AnotherObject> items;
    public List<AnotherObject> Items
    {
      return items.AsEnumerable().ToList<AnotherObject>();
    }
  }

I use NHibernate as my DAL and bind it directly to the items field, and all this works fine.

I also use Windows Workflow, and replicator activity does not work with generic IList. ( http://social.msdn.microsoft.com/Forums/en-US/windowsworkflowfoundation/thread/2ca74b60-fd33-4031-be4b-17a79e9afe63 ) This basically forces me to use the List <> wrapper instead of IList <>. This, of course, violates the direct mapping of NHibernate, since the NHibernate IList implementation cannot be directly passed to the list.

** : Windows , , , IList.

/ . , -- NHibernate nhibernate, .

, XML. XML , - nhibernate. . AsEnumerable.ToList .Add.

- ? ?

** EDIT: NHibernate NHibernate.Collection.Generic.PersistentGenericBag, IList . , . , , , .

+5
5

. - CustomList, , IList

:

public CustomList<AnotherObject> Items    
{      
    return new CustomList<AnotherObject>(items); 
}

. CustomList<T>, .

, IList, IList<T>, .

+2

, , . ToList() , , , ( ).

NHibernate, , IList ( ). , , , System.Collections.IList - , ( List<T>, ). IList?

+1

?

public class MyObject
{
    private IList<AnotherObject> items;
    public List<AnotherObject> Items()
    {
        return (List<AnotherObject>)items;
    }
}

Havent , , , !

0

I think the NHibernate PersistentBag (non-generic) collection implements IListso you can enter elements as IListinstead IList<AnotherObject>. The link in your question says that the problem is that the replicator requires an IList, which is implemented List<T>but IList<T>not working (go figure).

0
source

Can it be ported to IEnumerable <T>? You can try the following:

public class MyObject
{
    private IList<AnotherObject> items;
    public List<AnotherObject> Items
    {
        return new List<AnotherObject>items.Cast<AnotherObject>());
    }
    // or, to prevent modifying the list
    public IEnumerable<AnotherObject> Items
    {
        return items.Cast<AnotherObject>();
    }
}
0
source

All Articles