C# 3.0 自动属性 ​​- 是否可以添加自定义行为? [英] C# 3.0 Auto-Properties - Is it possible to add custom behaviour?

查看:21
本文介绍了C# 3.0 自动属性 ​​- 是否可以添加自定义行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以将自定义行为添加到自动属性 ​​get/set 方法中.

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

我能想到的一个明显的例子是希望每个设置的属性方法调用任何 PropertyChanged 事件处理程序作为 System.ComponentModel.INotifyPropertyChanged 实现的一部分.这将允许一个类具有许多可以观察的属性,其中每个属性都使用自动属性语法定义.

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.

基本上,我想知道是否有任何类似于 get/set 模板或具有类范围的 post get/set 钩子.

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)

推荐答案

不,您必须为自定义行为使用传统"属性定义.

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

这篇关于C# 3.0 自动属性 ​​- 是否可以添加自定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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