I'm debugging some code from the Microsoft prism library. Namely a class named ModuleCatalog:
public class AppModuleCatalog : IAppModuleCatalog
{
private readonly ModuleCatalogItemCollection _items;
private bool _isLoaded;
public Collection<IAppModuleCatalogItem> Items
{
get { return (Collection<IAppModuleCatalogItem>)this._items; }
}
public AppModuleCatalog()
{
this._items = new ModuleCatalogItemCollection();
this._items.CollectionChanged += new NotifyCollectionChangedEventHandler(this.ItemsCollectionChanged);
}
public virtual void AddModule(AppModuleInfo moduleInfo)
{
Argument.IsNotNull("moduleInfo", moduleInfo);
this.Items.Add(moduleInfo);
}
public AppModuleCatalog AddModule(string moduleName, string moduleType, string refValue, InitializationMode initializationMode, params string[] dependsOn)
{
Argument.IsNotNull("ModuleName", moduleName);
Argument.IsNotNull("moduleType", moduleType);
AppModuleInfo moduleInfo = new AppModuleInfo(moduleName, moduleType);
moduleInfo.DependsOn.AddRange(dependsOn);
moduleInfo.InitializationMode = initializationMode;
moduleInfo.Ref = refValue;
this.Items.Add(moduleInfo);
return this;
}
private void ItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (!this.Validated)
return;
this.EnsureCatalogValidated();
}
.
.
.
private class ModuleCatalogItemCollection : Collection<IAppModuleCatalogItem>, INotifyCollectionChanged
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected override void InsertItem(int index, IAppModuleCatalogItem item)
{
InsertItem(index,item);
this.OnNotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
}
protected void OnNotifyCollectionChanged(NotifyCollectionChangedEventArgs eventArgs)
{
if (this.CollectionChanged == null) return;
this.CollectionChanged(this, eventArgs);
}
}
}
When this class is initialized, an AddModule method is called that will add the ModuleInfo to Items property. What I have noticed is that the private class is then called and a recursive call is repeatedly made at the InsertItem method which of course causes an OverFlow Exception. Note that the getter from Items property returns _items
Could you please explain to me why the recursion is occurring?
According to the original source code , the ModuleCatalogItemCollection.InsertItem(int, IModuleCatalogItem)
method implementation looks like this:
protected override void InsertItem(int index, IModuleCatalogItem item)
{
base.InsertItem(index, item);
this.OnNotifyCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
}
Note the base.InsertItem(index, item)
method call. This will call the base method implementation from the Collection<IModuleCatalogItem>
class, not this.InsertItem()
. So there is no recursive call at this place.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.