如何注入方法在.Net框架汽车财产 [英] How to inject method to auto property in .Net Framework

查看:149
本文介绍了如何注入方法在.Net框架汽车财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类Foo具有许多特性:

I have some class Foo with many properties:

public class Foo
{
    public int Property1 { get; set; }

    public int Property2 { get; set; }

    public int Property3 { get; set; }
}

在其它类我有一些方法,如:

In other class I have some method, e.g.

public void SomeMethod()
{
    //...
}

如何注入这种方法在类Foo属性每二传手?
我使用.Net框架2.0

How to inject this method to every setter of properties in the class Foo? I use .Net Framework 2.0

推荐答案

我找到简单的方法来做到这一点。我使用 EasyProp ,它采用城堡的 DynamicProxy

I found easy way to do this. I use EasyProp, which uses Castle DynamicProxy:

我的类:

[AfterPropertySetFilter(typeof(NotifyPropertyChanged))]
public class Foo : INotifyPropertyChanged
{
    public virtual int Property1 { get; set; }

    public virtual int Property2 { get; set; }

    public virtual int Property3 { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
}



例如:

Example of use:

    EasyPropBuilder epb=new EasyPropBuilder();
    Foo foo = epb.Build<Foo>();
    foo.Property1 = 1;
    foo.PropertyChanged += OnPropertyChanged;
    foo.Property1 = 2;



此外,你需要增加这样的方法:

Also you need to add such method:

public static void OnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs)
{
    Console.WriteLine("Property Changed: " + propertyChangedEventArgs.PropertyName);
}

这篇关于如何注入方法在.Net框架汽车财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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