c#绝顶高手进

namespace WindowsFormsApplication1{    public partial class Myform : Form    {        public Myform()        {            InitializeComponent();            Initial();        }         public UiBindList _list { get; set; }         private void Initial()        {            _list = new UiBindList { SynchronizationContexts = SynchronizationContext.Current };            dataGridView1.DataBindings.Add("DataSource", this, "_list", false, DataSourceUpdateMode.OnPropertyChanged);             new Thread(() =>            {                while (true)                {                    Thread.Sleep(1000);                    _list.Add(new OBJ { Name = "C#" });                }            })            {                IsBackground = true,            }            .Start();        }    }     public class UiBindList : BindingList    {        public SynchronizationContext SynchronizationContexts { get; set; }         public void Excute(Action action, object state = null)        {            if (SynchronizationContexts == null)                action();            else                SynchronizationContexts.Post(p => action(), state);        }         public new void Add(T item)        {            Excute(() => base.Add(item));        }         public new void Remove(T item)        {            Excute(() => base.Remove(item));        }    }     public class OBJ { public string Name { get; set; } } 在Add方式中 报错!!对象的当前状态使该操作无效。
手掌心
浏览 438回答 4
4回答

慕妹3146593

public new void Add(T item)         {             Excute(() => Add(item));         }         public new void Remove(T item)         {             Excute(() => Remove(item));         }这两个方法去掉base.的引用就好了,改为上面的调用

一只甜甜圈

_list.Add(new OBJ(){Name="C#"}); 是不是这里少写了小括号?

慕桂英3389331

c#3.0就可以这么写了。对象自动化。。没有问题的。

慕容3067478

while (true)                {                    Thread.Sleep(1000);                    _list.Add(new OBJ { Name = "C#" });                } _list.Add(new OBJ { Name = "C#" });这句没有写括号,_list.Add(new OBJ(){Name="C#"});
打开App,查看更多内容
随时随地看视频慕课网APP