[英]Recursion with C# Event
I'm debugging some code from the Microsoft prism library. 我正在调试Microsoft棱镜库中的一些代码。 Namely a class named ModuleCatalog:
即一个名为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. 初始化此类后,将调用AddModule方法,该方法会将ModuleInfo添加到Items属性。 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.
我注意到的是,然后调用了私有类,并在InsertItem方法上反复进行了递归调用,这当然会导致OverFlow Exception。 Note that the getter from Items property returns _items
请注意,Items属性的getter返回_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: 根据原始源代码 ,
ModuleCatalogItemCollection.InsertItem(int, IModuleCatalogItem)
方法实现如下所示:
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. 请注意
base.InsertItem(index, item)
方法调用。 This will call the base method implementation from the Collection<IModuleCatalogItem>
class, not this.InsertItem()
. 这将从
Collection<IModuleCatalogItem>
类而不是this.InsertItem()
调用基本方法实现。 So there is no recursive call at this place. 因此,此位置没有递归调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.