PostSharp - Automatically Implementing INotifyPropertyChanged

要用 PostSharp 自動實作 INotifyPropertyChanged,在安裝完 PostSharp 擴充套件後,我們可以在類別上直接按下右鍵,在彈出的滑鼠右鍵快顯選單中,選取 Implement INotifyPropertyChanged 選單選項。


接著 PostSharp 擴充套件會帶出精靈視窗,第一頁是 Summary 頁面,這邊只是告訴我們繼續下去會做什麼事,不外乎就是加入套件引用與加上對應的 Attribute,這邊直接 Next。


如 Summary 頁面所提,要處理的東西有點多,進度要稍微跑一下。


進度跑完後按下 Finish 按鈕結束精靈視窗即可。


回過頭來看一下我們的專案,可以發現缺少的 PostSharp 參考被加入專案,且我們的類別上面被加上了 NotifyPropertyChanged 的 Attribute (可參閱 NotifyPropertyChangedAttribute Class,套上這 Attribute 會讓該類別及其子類都實作 INotifyPropertyChanged)。


到這邊已經用 PostSharp 幫我們自動實作完 INotifyPropertyChanged 了,若有需要可將滑鼠移至類別上再次進行確認,沒意外的話就會看到確實已經套用完成(若沒有這畫面可能是因為資料還未更新完成,可嘗試重建專案看看)。


若再次用反射進行解析。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;
using System. Collections.Generic ;
using System. Linq;
using System. Text;
using PostSharp. Patterns.Model ;


namespace ConsoleApplication32
{
class Program
{
static void Main( string[] args )
{
var blog = new Blog ("LevelUp", "http://larrynung.github.io" );


foreach (var @interface in blog .GetType() .GetInterfaces())
Console.WriteLine (@interface. Name);
}
}


[ NotifyPropertyChanged ]
public class Blog
{
public string Name { get; set ; }
public string Url { get; set ; }
public Blog (string name, string url)
{
this.Name = name;
this.Url = url;
}
}
}


可看到該介面確實已經實作。


最後一提,上述介紹的是用精靈介面下去操作,若不想使用精靈介面亦可,只要留意透過精靈介面對專案造成了什麼影響就應該知道怎麼處理了。簡單的說只要引用 PostSharp.Patterns.Model 套件,並將類別加上 NotifyPropertyChangedAttribute 即可。