带有ListChanged事件的StackOverflowException [英] StackOverflowException with the ListChanged event

查看:61
本文介绍了带有ListChanged事件的StackOverflowException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello group ...


我创建了一个实现以下接口的集合类:IBindingList,IList,ICollection,IEnumerable和ITypedList:


抽象类DataCollectionBase:IBindingList,IList,ICollection,IEnumerable,ITypedList

{

private ArrayList innerList = null;

private System.Type itemType = null;


public DataCollectionBase()

{

this.innerList = new ArrayList();

}


protected System.Type ItemType

{

get {return this.itemType; }

set {this.itemType = value; } $ / $
}


//列出更改的通知

protected virtual void OnInsertComplete(int index,object newValue)

{

if(ListChanged!= null)

{

ListChangedEventArgs args = new ListChangedEventArgs(ListChangedType.ItemAdded,index, - 1);

ListChanged(this,args); //抛出StackOverflowException

}

}


// IList实现

int IList.Add(对象值)

{

int index = this.innerList.Add(value);

OnInsertComplete(index,value) ;

}


// IBindingList实现

公共事件ListChangedEventHandler ListChanged = null;


对象IBindingList.AddNew()

{

对象newItem = null;


//这里我创建新的具有Reflection的项目,基于ItemType属性并在''newItem'''中存储引用。


((IList)this).Add(newItem);

返回newItem;

}

}


我正在使用我的集合作为DataSource的值DataGrid控件中的属性。

IBindingList.AddNew()方法工作正常,新项目通过Reflection实例化,但是当我在其中调用ListChanged事件处理程序时OnInsertComplete(..)方法,抛出StackOverflowException。

我不明白我的集合发生了什么,DataGrid控件是唯一消耗ListChanged事件的组件。


我真的需要你的帮助...


谢谢大家...

Hello group...

I''ve created a collection class that implements the following interfaces: IBindingList, IList, ICollection, IEnumerable and ITypedList:

abstract class DataCollectionBase : IBindingList, IList, ICollection, IEnumerable, ITypedList
{
private ArrayList innerList = null;
private System.Type itemType = null;

public DataCollectionBase()
{
this.innerList = new ArrayList();
}

protected System.Type ItemType
{
get { return this.itemType; }
set { this.itemType = value; }
}

// List Changed Notification
protected virtual void OnInsertComplete(int index, object newValue)
{
if (ListChanged != null)
{
ListChangedEventArgs args = new ListChangedEventArgs(ListChangedType.ItemAdded, index, -1);
ListChanged(this, args); // The StackOverflowException is thrown here
}
}

// IList Implementation
int IList.Add(object value)
{
int index = this.innerList.Add(value);
OnInsertComplete(index, value);
}

// IBindingList Implementation
public event ListChangedEventHandler ListChanged = null;

object IBindingList.AddNew()
{
object newItem = null;

// Here I create the new item with Reflection, based on the ItemType property and store a reference in ''newItem''''.

((IList)this).Add(newItem);
return newItem;
}
}

I''m using my collection as a value for the DataSource property in a DataGrid control.
The IBindingList.AddNew() method works fine and the new item is instantiated via Reflection, but when I invoke the ListChanged event handler within the OnInsertComplete(..) method, the StackOverflowException is thrown.
I don''t understand what''s happening to my collection, the DataGrid control is the only component that''s consuming the ListChanged event.

I really need your help...

Thank you all...

推荐答案

你能告诉我们ListChanged处理程序吗? StackOverflow =>一些(无意的,

意外)循环,每次迭代都会增加一些东西?
Can you show us the ListChanged handler? StackOverflow => some (unintended,
unanticipated) loop that adds something with each iteration?


你好Joep ......


ListChangedEventHandler是.NET Framework的一部分,它在System.ComponentModel命名空间下声明为

,如下所示:


public delegate void ListChangedEventHandler(对象发送者,

ListChangedEventArgs e);


和IBindingList接口包含该类型的事件。


接口IBindingList:IList,ICollection,IEnumerable

{

//属性转到这里


事件ListChangedEventHandler ListChanged;


//方法转到这里

}


因此,当我的类实现IBindingList接口时,我只需要写这个:b $ b这样的东西:


类DataCollectionBase:IBindingList,IList,ICollection,IEnumerable,

ITypedList

{

publi c事件ListChangedEventHandler ListChanged = null;


protected virtual void OnInsertComplete(int index,object newValue)

{

if(ListChanged != null)

{

ListChangedEventArgs args = new

ListChangedEventArgs(ListChangedType.ItemAdded,index,-1);

ListChanged(this,args); //引发StackOverflowException

这里

}

}

}


正如你所看到的,(从我的角度来看)我没有做任何奇怪的事情

事件。

只有DataGrid控件消耗了幕后的事件与

类似:


((IBindingList)DataSource).ListChanged + = new

ListChangedEventHandler(some_method );


至少这就是我的想法,如果我错了,请帮我吧......


谢谢再一次...


Joep <圣*** @ DeStoep.nl> escribióenel mensaje

news:41 ********************* @ news.xs4all.nl ...
Hello Joep...

The ListChangedEventHandler is part of the .NET Framework and it''s declared
under the System.ComponentModel namespace as follows:

public delegate void ListChangedEventHandler(object sender,
ListChangedEventArgs e);

and the IBindingList interface includes an event of that type.

interface IBindingList : IList, ICollection, IEnumerable
{
// Properties go here

event ListChangedEventHandler ListChanged;

// Methods go here
}

So, when my class implements the IBindingList interface I just have to write
something like this:

class DataCollectionBase : IBindingList, IList, ICollection, IEnumerable,
ITypedList
{
public event ListChangedEventHandler ListChanged = null;

protected virtual void OnInsertComplete(int index, object newValue)
{
if(ListChanged != null)
{
ListChangedEventArgs args = new
ListChangedEventArgs(ListChangedType.ItemAdded, index, -1);
ListChanged(this, args); // The StackOverflowException is thrown
here
}
}
}

As you can see, (from my point of view) I''m not doing anything strange with
the event.
Only the DataGrid control consumes the event behind the scenes with
something like:

((IBindingList)DataSource).ListChanged += new
ListChangedEventHandler(some_method);

At least that''s what I think, please right me if I''m wrong...

Thank you all again...

"Joep" <St***@DeStoep.nl> escribió en el mensaje
news:41*********************@news.xs4all.nl...
你能告诉我们ListChanged处理程序吗? StackOverflow =>一些
(非预期的,未预料到的)循环,每次迭代都会添加一些东西?
Can you show us the ListChanged handler? StackOverflow => some
(unintended, unanticipated) loop that adds something with each iteration?



你能否提供堆栈跟踪,如果可能的话,还可以提供一个简短但是

完整的程序[1]展示你的问题?

可能你的应用程序中有其他东西是

导致问题。减少您的代码,直到它只是导致问题所需的代码

。虽然,与文章相反,在这个

的情况下,你会提供一个GUI。


1. http://yoda.arachsys.com/csharp/complete.html (由Jon Skeet撰写,另一篇

C#MVP。我之所以提到它只是因为页面本身是第一人写的

并且似乎没有提及作者是谁。)
Could you perhaps provide the stack trace and, if possible, a short but
complete program[1] that exhibits your issue?
Its possible that there is something else in your application that is
causing the issue. Whittle down your code until it is nothing but the code
required to cause the problem. Although, contrary to the article, in this
case you would provide a GUI.

1. http://yoda.arachsys.com/csharp/complete.html (Its by Jon Skeet, another
C# MVP. I mention it only because the page itself is written in first person
and doesn''t seem to mention who the author is.)


这篇关于带有ListChanged事件的StackOverflowException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆