Skip to content

Wrong CopyTo implementationΒ #1660

@lucaslorentz

Description

@lucaslorentz

It seems that most ICollection.CopyTo implementations on Nhibernate are wrong.

For example at:
https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Collection/Generic/PersistentGenericBag.cs#L95

Index parameter is the index on the target array the data should be copied to. So, the correct implementation would be something like:

void ICollection.CopyTo(Array array, int index)
{
  for (var i = 0; i < Count; i++)
  {
    array.SetValue(this[i], index + i);
  }
}

I detected that after facing issues with .NET Core methods that uses CopyTo.

SelectMany fails:

var bags = new[]
{
    new PersistentGenericBag<string>(session, new [] {
        "A"
    }),
    new PersistentGenericBag<string>(session, new[] {
        "B"
    })
};

var items = bags.SelectMany(b => b).ToArray();
//Items should be ["A", "B"], but it is ["A", null]

Lists AddRange fails:

var list = new List<string>();
list.Add("A");

var bag = new PersistentGenericBag<string>(session, new[] {
  "B"
});

list.AddRange(bag);

//Items should be ["A", "B"], but it is ["A", null]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions