フィルター属性の自作/例外の自作
Serializable属性→今後の課題
リスト6-73
Imports System
Imports System.Web.Mvc
Public Class TimeLimitAttribute
Inherits FilterAttribute
Implements IAuthorizationFilter
Private _begin As DateTime = DateTime.MinValue
Private _end As DateTime = DateTime.MaxValue
Public WriteOnly Property Begin_Property As String
Set(value As String)
Dim b = DateTime.Parse(value)
If b >= Me._end Then
Throw New ArgumentException("Begin parameter is invalid")
End If
Me._begin = b
End Set
End Property
Public WriteOnly Property End_Property As String 'Endは予約語
Set(value As String)
Dim e = DateTime.Parse(value)
If Me._begin >= e Then
Throw New ArgumentException("End parmeter is invalid")
End If
Me._end = e
End Set
End Property
Public Sub New(ByVal begin As String, ByVal [end] As String) '予約語回避
Me.Begin_Property = begin
Me.End_Property = [end]
End Sub
Public Sub OnAuthorization(filterContext As AuthorizationContext) Implements IAuthorizationFilter.OnAuthorization
If IsNothing(filterContext) Then
Throw New ArgumentNullException("filterContext")
End If
Dim current = DateTime.Now
If current < Me._begin OrElse current > Me._end Then
Dim msg = String.Format("このページは{0}から{1}までの期間のみ有効です。",
Me._begin.ToLongDateString(), Me._end.ToLongDateString())
Throw New TimeLimitException(msg)
End If
End Sub
<Serializable>
Public Class TimeLimitException
Inherits Exception
Public Sub New(ByVal message As String)
MyBase.New(message)
End Sub
End Class
End Class
参考
//オブジェクトのシリアライズ
http://www.atmarkit.co.jp/fdotnet/vb6tonet/vb6tonet28/vb6tonet28_02.html
//新しい例外クラスを作成する
https://msdn.microsoft.com/ja-jp/library/vstudio/6sh1zxsd(v=vs.100).aspx