OGeek|极客世界-中国程序员成长平台

标题: .net - C# 3.0 Auto-Properties - Is it possible to add custom behaviour? [打印本页]

作者: 菜鸟教程小白    时间: 2022-6-1 20:09
标题: .net - C# 3.0 Auto-Properties - Is it possible to add custom behaviour?

I would like to know if there is any way to add custom behaviour to the auto property get/set methods.

An obvious case I can think of is wanting every set property method to call on any PropertyChanged event handlers as part of a System.ComponentModel.INotifyPropertyChanged implementation. This would allow a class to have numerous properties that can be observed, where each property is defined using auto property syntax.

Basically I'm wondering if there is anything similar to either a get/set template or post get/set hook with class scope.

(I know the same end functionality can easily be achieved in slightly more verbose ways - I just hate duplication of a pattern)



Best Answer-推荐答案


No, you'll have to use "traditional" property definitions for custom behavior.






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4