开发者

How to link a property setter to a delegate?

I would like to give a property setter to a delegate. How is this done?

class A {
  private int count;
  pu开发者_运维百科blic int Count {
    get { return count; }
    set { count = value; }
  }
}
A a = new A();   
delegate void ChangeCountDelegate(int x);
ChangeCountDelegate dlg = ... ? // should call a.Count = x


ChangeCountDelegate dlg = (int x) => a.Count = x;

// or
ChangeCountDelegate dlg = x => a.Count = x;

// or 
ChangeCountDelegate dlg = new ChangeCountDelegate(delegate(int x) { a.Count = x; } );

// or 
ChangeCountDelegate dlg = new ChangeCountDelegate(int x => a.Count = x);

Or am I thinking to easy? :)

I'm sure you get the point.

The 3rd one works in .NET 2.0, the others need at least 3.5 :)


Try this:

ChangeCountDelegate dlg = v => a.Count = v;


C# does not support Property-Delegates.

You can work with anonymous methods in the way Snake mentioned if you need to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜