问题描述
有没有办法避免这种情况。我有很多类绑定到DataGridViews,它们只是使用默认的getter和setter简单的属性集合。所以这些课很简单。现在我需要为它们实现INotifyPropertyChanged接口,这将增加代码量。有没有可以继承的类,以避免编写所有这些无聊的代码?我的形象,我可以继承我的类从一些类,并装饰属性与一些属性,它会做的魔术。这是可能的吗?
我很了解面向对象编程,但我宁愿做面向对象的方式。
创建容器基类,例如:
abstract class Container :INotifyPropertyChanged
{
字典< string,object>价值观
protected object this [string name]
{
get {return values [name]; }
set
{
values [name] = value;
PropertyChanged(this,new PropertyChangedEventArgs(name));
}
}
}
class Foo:Container
{
public int Bar
{
{get {return(int)this [Bar]; }}
{set {this [Bar] = value; }}
}
}
注意:非常简化的代码
Is there some way to avoid this. I have a lot of classes that are bound to DataGridViews and they are just simple collection of properties with default getter and setter. So these classes are very simple. Now I need to implement INotifyPropertyChanged interface for them which will increase the amount of code a lot.Is there any class that I can inherit from to avoid writing all this boring code? I image that I can inherit my classes from some class and decorate the properties with some attributes and it will do the magic. Is that possible?
I'm well aware of Aspect Oriented Programming, but I'd rather do it object oriented way.
Create a container base class, eg:
abstract class Container : INotifyPropertyChanged
{
Dictionary<string, object> values;
protected object this[string name]
{
get {return values[name]; }
set
{
values[name] = value;
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
class Foo : Container
{
public int Bar
{
{get {return (int) this["Bar"]; }}
{set { this["Bar"] = value; } }
}
}
Note: very simplified code
这篇关于如何避免手动实现INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!