I've implemented an event that is fired every time a value changes in a class. I also have a class that has a collection of those items and I'd like to subscribe to those items events. I'm trying to do s开发者_运维知识库o in the setter of a property like this:
public virtual ISet<ItemType> items
{
get
{
return this._items;
}
set
{
this._items = value;
foreach (var item in this._items)
{
item.PropertyChanged += this.Item_ThePropertyChanged;
}
}
}
But I get an "illegal access to loading collection" error as soon as we reach the "in this._items" from the "for" part. Here's the stack trace:
at NHibernate.Collection.AbstractPersistentCollection.Initialize(Boolean writing)\r\n at NHibernate.Collection.AbstractPersistentCollection.Read()\r\n at NHibernate.Collection.Generic.PersistentGenericSet`1.System.Collections.Generic.IEnumerable.GetEnumerator()\r\n at MyMethod
Thanks in advance for any help
Apparently NHibernate blocks access to the collection while it is initializing the property. Does it help to change the foreach
loop to a regular for
loop? It may be that only the GetEnumerator()
method is blocked:
for (var i = 0; i < _items.Count; i++)
{
_items[i].PropertyChanged += Item_ThePropertyChanged;
}
精彩评论