PostSharp - Contract Inheritance

PostSharp 的 Contract 跟 Conde Contract 一樣,具備可被繼承的特性。凡是套用在 abstract、virtual、或 interface 方法上的 Contract,其子類別都會繼承到,在開發上十分的好用。


這邊來看個例子,筆者撰寫了個 IWritable 的介面,在其 Write 方法上我們加上了 RequiredAttribute,再建立個 Blog 類別去實作該介面。

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PostSharp.Patterns.Contracts;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IWriteable blog = new Blog("LevelUp", "http://larrynung.github.io");
blog.Write("PostSharp Contract Inheritance", "Hello~Contract");
blog.Write("", "");
}
}

public interface IWritable
{
void Write([Required]string title, string content);
}

public class Blog : IWritable
{
public string Name
{
get;
set;
}

public string Url
{
get;
set;
}

public Blog(string name, string url)
{
this.Name = name;
this.Url = url;
}

public void Write(string title, string content)
{
Console.WriteLine(String.Format("== {0} =={1}{2}", title, Environment.NewLine, content));
}
}
}


當我們呼叫 Blog.Write 時,因為 Blog 類別從 IWritable 介面繼承了 Write 方法及其 Contract,所以叫用時若帶的是空值就會丟出例外。