Author:水如烟
MSDN帮助文档如是说:
ContextBoundObject 类
定义所有上下文绑定类的基类。
备注
驻留在上下文中并要绑定到上下文规则的对象称为上下文绑定对象。上下文是用于定义对象集合所驻留的环境的属性集或用法规则集。当对象进入或离开上下文时,规则被强制。非上下文绑定的对象称为灵活对象。
上下文在对象激活期间创建。新对象被放置到现有上下文或新的上下文中,后者是通过元数据类型中包含的属性创建的。使用提供用法规则的 ContextAttribute 标记上下文绑定类。可以添加的上下文属性包括有关同步和事务的策略。
ContextAttribute 类
该类型支持 .NET Framework 结构,因此不适用于直接从代码中使用。
那是说,用ContextAttribute标记的继承了ContextBoundObject的类,有自身的规则。继承了ContextBoundObject的类,不晓得在哪处会另有玄机,不要轻信代码本身表现的逻辑。
这里用个例子来说说。
一个会场,一个通道,门卫只对名字有“8”的人放行。
假设名字为Guest000,Guest001,...,Guest099共100人尝试进场,最后到了会场的有什么人。
为此,一般的,建一个MeetingPlace项目,输出为Dll,有以下三个文件:
User.vb
Private _Name As String = " guest "
Private _Valid As Boolean = False
Public Property Name() As String
Get
Return _Name
End Get
Set ( ByVal value As String )
_Name = value
End Set
End Property
Public Sub SetValid( ByVal valid As Boolean )
Me ._Valid = valid
End Sub
Public ReadOnly Property Valid() As Boolean
Get
Return _Valid
End Get
End Property
Public Overrides Function ToString() As String
Return String .Format( " {0}:{1} valid " , Name, IIf (Valid, "" , " not " ))
End Function
End Class
通道和门卫(放行规则)
Passage.vb
Public Sub Check( ByVal user As User)
If user.Name.IndexOf( " 8 " ) <> - 1 Then
user.SetValid( True )
End If
End Sub
End Class
进场情形
Center.vb
Private ValidUserList As List(Of User)
Private passage As Passage
Sub New ()
ValidUserList = New List(Of User)
passage = New Passage
End Sub
Public Sub test()
Dim mUser As User
For i As Integer = 0 To 99
mUser = New User
mUser.Name = " Guest " & i.ToString.PadLeft( 3 , " 0 " )
Me .Guard(mUser)
Next
End Sub
Public Sub Guard( ByVal user As User)
passage.Check(user)
If user.Valid Then
ValidUserList.Add(user)
End If
End Sub
Public ReadOnly Property ValidUsers() As User()
Get
Return ValidUserList.ToArray
End Get
End Property
End Class
现在引用这个Dll使用它,结果肯定与预测相符:
Dim mCenter As New MeetingPlace.Center
mCenter.test()
Me .ListBox1.DataSource = mCenter.ValidUsers
End Sub
但是,如果放行规则文件Passage.vb是这样:
< SimpleMethodWatcher() > < SimpleMethodWatcherAppend() > _
Public Class Passage
Inherits ContextBoundObject
Public Sub Check( ByVal user As User)
If user.Name.IndexOf( " 8 " ) <> - 1 Then
user.SetValid( True )
End If
End Sub
End Class
就继承了ContextBoundObject,加了两个标志,这样还能保证进场的人与预测相符吗?不能了。有可能全部进了场,有可能一个不能进,有可能Guest007也进去了,还有可能通道给封闭了,一验就发生异常。总之,充满了不可预测性,玄得很哪。
关于SimpleMethodWatcher和SimpleMethodWatcherAppend见MethodWather。文的最后也有全部代码下载。
那样做后,生成了Dll.
在引用使用这个dll 时,我可以这样来实现新的规则。而这个规则不再理睬原先的Check了。
MyGuard.vb
Imports System.Runtime.Remoting.Messaging
Public Class MyGuard
Inherits MethodWatcherCenter
Private _pass As PassType
Public WriteOnly Property Pass() As PassType
Set ( ByVal value As PassType)
_pass = value
End Set
End Property
Public Sub Ready()
Remove()
AddHandler MethodCallBegin, AddressOf WriteMethodCallBegin
AddHandler MethodCallOver, AddressOf WriteMethodCallOver
End Sub
Public Sub Remove()
RemoveHandler MethodCallBegin, AddressOf WriteMethodCallBegin
RemoveHandler MethodCallOver, AddressOf WriteMethodCallOver
End Sub
Protected Overridable Sub WriteMethodCallBegin( ByVal callMsg As IMethodCallMessage, ByVal MethodAppendType As System.Type)
If MethodAppendType.Equals( GetType (SimpleMethodWatcherAppendAttribute)) Then
Dim mUser As MeetingPlace.User = CType (callMsg.GetArg( 0 ), MeetingPlace.User)
Select Case _pass
Case PassType.Normal
Case PassType.All
mUser.SetValid( True )
Case PassType.None
Case PassType.Let007in
If mUser.Name = " Guest007 " Then
mUser.Name = " 8 " & mUser.Name
End If
Case PassType.Exception
Throw New Exception
End Select
Else
End If
End Sub
Protected Overridable Sub WriteMethodCallOver( ByVal replyMsg As IMethodReturnMessage, ByVal MethodAppendType As System.Type)
If MethodAppendType.Equals( GetType (SimpleMethodWatcherAppendAttribute)) Then
Dim mUser As MeetingPlace.User = CType (replyMsg.GetArg( 0 ), MeetingPlace.User)
Select Case _pass
Case PassType.Normal
Case PassType.All
Case PassType.None
mUser.SetValid( False )
Case PassType.Let007in
If mUser.Name = " 8Guest007 " Then
mUser.Name = mUser.Name.Substring( 1 )
End If
Case PassType.Exception
End Select
Else
End If
End Sub
End Class
Public Enum PassType
Normal
All
None
Let007in
Exception
End Enum
界面代码:
Private myGuard As New MyGuard
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mCenter As New MeetingPlace.Center
mCenter.test()
Me .ListBox1.DataSource = mCenter.ValidUsers
End Sub
Private Sub Button2_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
myGuard.Ready()
End Sub
Private Sub Button3_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
myGuard.Remove()
End Sub
Private Sub ComboBox1_SelectedIndexChanged( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
myGuard.Pass = CType ([ Enum ].Parse( GetType (PassType), Me .ComboBox1.Text), PassType)
End Sub
Private Sub Form1_Load( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Load
Me .ComboBox1.DataSource = [ Enum ].GetNames( GetType (PassType))
End Sub
End Class