【发布时间】:2011-06-27 22:41:45
【问题描述】:
如果我对这一行的内部运作的理解是正确的:
public int MyInt { get; set; }
然后它在幕后这样做:
private int _MyInt { get; set; }
Public int MyInt {
get{return _MyInt;}
set{_MyInt = value;}
}
我真正需要的是:
private bool IsDirty { get; set; }
private int _MyInt { get; set; }
Public int MyInt {
get{return _MyInt;}
set{_MyInt = value; IsDirty = true;}
}
但我想这样写:
private bool IsDirty { get; set; }
public int MyInt { get; set{this = value; IsDirty = true;} }
这不起作用。问题是我需要执行 IsDirty 的一些对象有几十个属性,我希望有一种方法可以使用自动 getter/setter,但在修改字段时仍然设置 IsDirty。
这可能吗,还是我只需要辞职以将课程中的代码量增加三倍?
【问题讨论】:
-
改用 IronPython。它在 CLR 上,但支持面向方面的编程,这正是您所需要的。 :)
-
如果您使用 Visual Studio,您可以创建一个代码 sn-p 以帮助您至少节省时间。
标签: c# getter-setter shorthand